Ejemplo n.º 1
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.º 2
0
        public void Draw(DeviceContextHolder holder, ShaderResourceView view, RenderTargetView target, 
                TargetResourceTexture temporaryEdges, TargetResourceTexture temporaryBlending) {
            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);
        }
Ejemplo n.º 3
0
 public void Draw(DeviceContextHolder holder, TargetResourceDepthTexture from, TargetResourceTexture to) {
     holder.DeviceContext.Rasterizer.SetViewports(to.Viewport);
     holder.DeviceContext.OutputMerger.SetTargets(to.TargetView);
     holder.PrepareQuad(_effect.LayoutPT);
     _effect.FxInputMap.SetResource(from.View);
     _effect.TechCopy.DrawAllPasses(holder.DeviceContext, 6);
 }
Ejemplo n.º 4
0
        public override void Draw(DeviceContextHolder holder, ShaderResourceView depth, ShaderResourceView normals, ICamera camera, RenderTargetView target)
        {
            base.Draw(holder, depth, normals, camera, target);

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

            // hbao
            var c = camera as BaseCamera;

            if (c == null)
            {
                return;
            }

            //_effect.FxWorldViewProjInv.SetMatrix(c.ViewProjInvert);
            //_effect.FxView.SetMatrix(c.View);

            var proj = c.Proj; // Matrix.PerspectiveFovLH(c.FovY, c.Aspect, c.NearZValue, c.FarZValue);
            var view = c.View; // Matrix.LookAtRH(c.Position.FlipX(), (c.Position + c.Look).FlipX(), c.Up.FlipX());

            //view = Matrix.LookAtLH(c.Position, (c.Position + c.Look), c.Up);

            _effect.FxWorldViewProjInv.SetMatrix(Matrix.Invert(view * proj));
            _effect.FxView.SetMatrix(view);

            _effect.FxProjectionMatrix.SetMatrix(proj);
            _effect.FxProj.SetMatrix(proj);
            _effect.FxProjT.SetMatrix(Matrix.Transpose(proj));
            _effect.FxProjInv.SetMatrix(Matrix.Invert(proj));
            _effect.FxDitherScale.Set(holder.Width / 16);
            _effect.FxRenderTargetResolution.Set(new Vector2(holder.Width, holder.Height));

            var corners = new [] {
                new Vector4(-1f, -1f, 0f, 1f),
                new Vector4(1f, -1f, 0f, 1f),
                new Vector4(1f, 1f, 0f, 1f),
                new Vector4(-1f, 1f, 0f, 1f)
            };

            var inverseProjection = Matrix.Invert(proj);

            for (var i = 0; i < 4; ++i)
            {
                corners[i]  = Vector4.Transform(corners[i], inverseProjection);
                corners[i] /= corners[i].W;
                corners[i] /= corners[i].Z;
            }

            _effect.FxViewFrustumVectors.Set(corners);
            //_effect.FxNormalsToViewSpace.SetMatrix(Matrix.Invert(Matrix.Transpose(camera.ViewProj)));
            //_effect.FxNormalsToViewSpace.SetMatrix(Matrix.Invert(Matrix.Transpose(camera.Proj)));
            //_effect.FxNormalsToViewSpace.SetMatrix(view);
            //_effect.FxNormalsToViewSpace.SetMatrix(Matrix.Invert(Matrix.Transpose(view)));
            // _effect.Fx

            _effect.TechHbao.DrawAllPasses(holder.DeviceContext, 6);
        }
Ejemplo n.º 5
0
        /*public void AccumulateBokehDivide(DeviceContextHolder holder, ShaderResourceView view, ShaderResourceView maxView, RenderTargetView target, int amount,
                float bokehMultiplier) {
            holder.DeviceContext.OutputMerger.SetTargets(target);
            holder.PrepareQuad(_effect.LayoutPT);
            _effect.FxInputMap.SetResource(view);
            _effect.FxOverlayMap.SetResource(maxView);
            _effect.FxMultipler.Set(1f / amount);
            _effect.FxScreenSize.Set(new Vector4(holder.Width, holder.Height, 1f / holder.Width, 1f / holder.Height));
            _effect.FxBokenMultipler.Set(new Vector2(bokehMultiplier, 1f - bokehMultiplier));
            _effect.TechAccumulateBokehDivide.DrawAllPasses(holder.DeviceContext, 6);
        }*/

        public void Cut(DeviceContextHolder holder, ShaderResourceView view, RenderTargetView target, float cut) {
            holder.DeviceContext.OutputMerger.SetTargets(target);
            holder.PrepareQuad(_effect.LayoutPT);
            _effect.FxInputMap.SetResource(view);
            _effect.FxSizeMultipler.Set(1f / cut);
            _effect.TechCut.DrawAllPasses(holder.DeviceContext, 6);
        }
Ejemplo n.º 6
0
 public void Final(DeviceContextHolder holder, ShaderResourceView view, RenderTargetView target, bool checkedBackground)
 {
     holder.DeviceContext.OutputMerger.SetTargets(target);
     holder.PrepareQuad(_effect.LayoutPT);
     _effect.FxInputMap.SetResource(view);
     (checkedBackground ? _effect.TechFinalCheckers : _effect.TechFinal).DrawAllPasses(holder.DeviceContext, 6);
 }
Ejemplo n.º 7
0
        private float AddShadow(Vector3 lightDirection)
        {
            DeviceContext.OutputMerger.BlendState = _blendState;
            DeviceContext.Rasterizer.State        = null;
            DeviceContext.Rasterizer.SetViewports(Viewport);

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

            _effect.FxDepthMap.SetResource(_shadowBuffer.View);
            _effect.FxShadowViewProj.SetMatrix(_shadowDestinationTransform * _shadowCamera.ViewProj * new Matrix {
                M11 = 0.5f,
                M22 = -0.5f,
                M33 = 1.0f,
                M41 = 0.5f,
                M42 = 0.5f,
                M44 = 1.0f
            });

            var brightness = CorrectLighting ? lightDirection.Y.Abs() : 1f;

            _effect.FxMultipler.Set(brightness);
            _effect.TechAmbientShadow.DrawAllPasses(DeviceContext, 6);
            return(brightness);
        }
Ejemplo n.º 8
0
        protected override void DrawPrepare()
        {
            base.DrawPrepare();

            var highlighted = AmbientShadowHighlight ? (IRenderableObject)CarNode?.AmbientShadowNode : SelectedObject;

            if (highlighted == null || _outlineBuffer == null)
            {
                return;
            }

            DeviceContext.ClearDepthStencilView(_outlineDepthBuffer.DepthView, DepthStencilClearFlags.Depth | DepthStencilClearFlags.Stencil, 1f, 0);
            DeviceContext.OutputMerger.SetTargets(_outlineDepthBuffer.DepthView);
            DeviceContext.Rasterizer.State = DeviceContextHolder.States.DoubleSidedState;

            highlighted.Draw(DeviceContextHolder, ActualCamera, SpecialRenderMode.Outline);

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

            var effect = DeviceContextHolder.GetEffect <EffectPpOutline>();

            effect.FxDepthMap.SetResource(_outlineDepthBuffer.View);
            effect.FxScreenSize.Set(new Vector4(ActualWidth, ActualHeight, 1f / ActualWidth, 1f / ActualHeight));
            DeviceContextHolder.PrepareQuad(effect.LayoutPT);
            effect.TechOutline.DrawAllPasses(DeviceContext, 6);
        }
Ejemplo n.º 9
0
 public void AccumulateDivide(DeviceContextHolder holder, ShaderResourceView view, RenderTargetView target, int amount) {
     holder.DeviceContext.OutputMerger.SetTargets(target);
     holder.PrepareQuad(_effect.LayoutPT);
     _effect.FxInputMap.SetResource(view);
     _effect.FxMultipler.Set(1f / amount);
     _effect.TechAccumulateDivide.DrawAllPasses(holder.DeviceContext, 6);
 }
Ejemplo n.º 10
0
 public void DrawSqrt(DeviceContextHolder holder, ShaderResourceView view, RenderTargetView target)
 {
     holder.DeviceContext.OutputMerger.SetTargets(target);
     holder.PrepareQuad(_effect.LayoutPT);
     _effect.FxInputMap.SetResource(view);
     _effect.TechCopySqrt.DrawAllPasses(holder.DeviceContext, 6);
 }
        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);
            }
        }
Ejemplo n.º 12
0
 public void DepthToLinear(DeviceContextHolder holder, ShaderResourceView view, RenderTargetView target, float zNear, float zFar,
         float zFarNormalize) {
     holder.DeviceContext.OutputMerger.SetTargets(target);
     holder.PrepareQuad(_effect.LayoutPT);
     _effect.FxInputMap.SetResource(view);
     _effect.FxScreenSize.Set(new Vector4(zNear, zFar, zFarNormalize, 0));
     _effect.TechDepthToLinear.DrawAllPasses(holder.DeviceContext, 6);
 }
Ejemplo n.º 13
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.º 14
0
 public void AccumulateBokehDivide(DeviceContextHolder holder, ShaderResourceView view, ShaderResourceView maxView, RenderTargetView target, int amount,
                                   float bokehMultiplier)
 {
     holder.DeviceContext.OutputMerger.SetTargets(target);
     holder.PrepareQuad(_effect.LayoutPT);
     _effect.FxInputMap.SetResource(view);
     _effect.FxOverlayMap.SetResource(maxView);
     _effect.FxMultipler.Set(1f / amount);
     _effect.FxBokenMultipler.Set(new Vector2(bokehMultiplier, 1f - bokehMultiplier));
     _effect.TechAccumulateBokehDivide.DrawAllPasses(holder.DeviceContext, 6);
 }
Ejemplo n.º 15
0
        public void Draw(DeviceContextHolder holder, ShaderResourceView view, RenderTargetView target, TargetResourceTexture temporaryLuma) {
            holder.PrepareQuad(_effect.LayoutPT);

            holder.DeviceContext.OutputMerger.SetTargets(temporaryLuma.TargetView);
            _effect.FxInputMap.SetResource(view);
            _effect.TechLuma.DrawAllPasses(holder.DeviceContext, 6);

            holder.DeviceContext.OutputMerger.SetTargets(target);
            _effect.FxScreenSize.Set(new Vector4(holder.Width, holder.Height, 1f / holder.Width, 1f / holder.Height));
            _effect.FxInputMap.SetResource(temporaryLuma.View);
            _effect.TechFxaa.DrawAllPasses(holder.DeviceContext, 6);
        }
Ejemplo n.º 16
0
        private void Draw(float multipler, int size, int padding, float fadeRadius, [CanBeNull] IProgress <double> progress, CancellationToken cancellation)
        {
            DeviceContext.ClearRenderTargetView(_summBuffer.TargetView, Color.Transparent);

            // draw
            var yThreshold = 0.95f - DiffusionLevel * 0.95f;

            ΘFromDeg = (MathF.PI / 2f - yThreshold.Acos()).ToDegrees();
            ΘToDeg   = 90;

            var iter = DrawLights(progress, cancellation);

            if (cancellation.IsCancellationRequested)
            {
                return;
            }

            DeviceContextHolder.PrepareQuad(_effect.LayoutPT);
            DeviceContext.Rasterizer.State        = null;
            DeviceContext.OutputMerger.BlendState = null;
            DeviceContext.Rasterizer.SetViewports(Viewport);

            _effect.FxSize.Set(new Vector4(Width, Height, 1f / Width, 1f / Height));

            // blurring
            _effect.FxMultipler.Set(1f);
            for (var i = ExtraBlur ? 2 : 1; i > 0; i--)
            {
                DeviceContext.ClearRenderTargetView(_tempBuffer.TargetView, Color.Transparent);
                DeviceContext.OutputMerger.SetTargets(_tempBuffer.TargetView);

                _effect.FxInputMap.SetResource(_summBuffer.View);
                _effect.TechHorizontalShadowBlur.DrawAllPasses(DeviceContext, 6);

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

                _effect.FxInputMap.SetResource(_tempBuffer.View);
                _effect.TechVerticalShadowBlur.DrawAllPasses(DeviceContext, 6);
            }

            // result
            DeviceContext.ClearRenderTargetView(RenderTargetView, Color.Transparent);
            DeviceContext.OutputMerger.SetTargets(RenderTargetView);
            _effect.FxInputMap.SetResource(_summBuffer.View);
            _effect.FxCount.Set(iter / SkyBrightnessLevel);
            _effect.FxMultipler.Set(multipler);
            _effect.FxFade.Set(fadeRadius != 0f ? 10f / fadeRadius : 100f);
            _effect.FxPadding.Set(padding / (size + padding * 2f));
            _effect.FxShadowSize.Set(new Vector2(_shadowSize.X, _shadowSize.Z));
            _effect.FxScreenSize.Set(new Vector2(ActualWidth, ActualHeight));
            _effect.TechResult.DrawAllPasses(DeviceContext, 6);
        }
Ejemplo n.º 17
0
        public void Draw(DeviceContextHolder holder, ShaderResourceView view, RenderTargetView target, bool hq)
        {
            holder.DeviceContext.OutputMerger.SetTargets(target);
            holder.PrepareQuad(_effect.LayoutPT);
            _effect.FxInputMap.SetResource(view);

            if (hq)
            {
                _effect.FxScreenSize.Set(new Vector4(holder.Width, holder.Height, 1f / holder.Width, 1f / holder.Height));
            }

            (hq ? _effect.TechCopyHq : _effect.TechCopy).DrawAllPasses(holder.DeviceContext, 6);
        }
Ejemplo n.º 18
0
        private void Draw(DeviceContextHolder holder, ShaderResourceView source, Vector2 sourceSize, RenderTargetView destination, Vector2 destinationSize, EffectReadyTechnique tech)
        {
            holder.DeviceContext.Rasterizer.SetViewports(new Viewport(0f, 0f, destinationSize.X, destinationSize.Y));

            holder.DeviceContext.OutputMerger.SetTargets(destination);
            holder.PrepareQuad(_effect.LayoutPT);
            _effect.FxInputMap.SetResource(source);

            _effect.FxScreenSize.Set(new Vector4(destinationSize.X, destinationSize.Y, 1f / destinationSize.X, 1f / destinationSize.Y));
            _effect.FxMultipler.Set(new Vector2(destinationSize.X / sourceSize.X, destinationSize.Y / sourceSize.Y));

            tech.DrawAllPasses(holder.DeviceContext, 6);
        }
Ejemplo 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);
                }
            }
        }
Ejemplo n.º 20
0
        public void Draw(DeviceContextHolder holder, ShaderResourceView source, Vector2 sourceSize, RenderTargetView destination, Vector2 destinationSize,
                         bool useFoundTech)
        {
            holder.DeviceContext.Rasterizer.SetViewports(new Viewport(0f, 0f, destinationSize.X, destinationSize.Y));

            holder.DeviceContext.OutputMerger.SetTargets(destination);
            holder.PrepareQuad(_effect.LayoutPT);
            _effect.FxInputMap.SetResource(source);

            _effect.FxScreenSize.Set(new Vector4(destinationSize.X, destinationSize.Y, 1f / destinationSize.X, 1f / destinationSize.Y));
            _effect.FxMultipler.Set(new Vector2(destinationSize.X / sourceSize.X, destinationSize.Y / sourceSize.Y));

            (useFoundTech ? _effect.TechFound : _effect.TechAverage).DrawAllPasses(holder.DeviceContext, 6);
        }
Ejemplo n.º 21
0
        public void Draw(DeviceContextHolder holder, ShaderResourceView depth, ShaderResourceView baseReflection, ShaderResourceView normals,
                         ICamera camera, RenderTargetView target)
        {
            holder.DeviceContext.OutputMerger.SetTargets(target);
            holder.PrepareQuad(_effect.LayoutPT);
            _effect.FxDepthMap.SetResource(depth);
            _effect.FxBaseReflectionMap.SetResource(baseReflection);
            _effect.FxNormalMap.SetResource(normals);

            _effect.FxEyePosW.Set(camera.Position);
            _effect.FxWorldViewProj.SetMatrix(camera.ViewProj);
            _effect.FxWorldViewProjInv.SetMatrix(camera.ViewProjInvert);

            _effect.TechSslr.DrawAllPasses(holder.DeviceContext, 6);
        }
Ejemplo n.º 22
0
        public override void Draw(DeviceContextHolder holder, ShaderResourceView depth, ShaderResourceView normals, ICamera camera, RenderTargetView target)
        {
            base.Draw(holder, depth, normals, camera, target);
            _effect.FxNoiseSize.Set(new Vector2(holder.Width / 4f, holder.Height / 4f));

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

            _effect.FxViewProj.SetMatrix(camera.ViewProj);
            _effect.FxViewProjInv.SetMatrix(camera.ViewProjInvert);

            _effect.TechSsaoVs.DrawAllPasses(holder.DeviceContext, 6);
        }
Ejemplo n.º 23
0
        public void FinalStep(DeviceContextHolder holder, ShaderResourceView colorMap, ShaderResourceView firstStep, ShaderResourceView baseReflection, ShaderResourceView normals,
                              ICamera camera, RenderTargetView target)
        {
            holder.DeviceContext.OutputMerger.SetTargets(target);
            holder.PrepareQuad(_effect.LayoutPT);
            _effect.FxDiffuseMap.SetResource(colorMap);
            _effect.FxFirstStepMap.SetResource(firstStep);
            _effect.FxBaseReflectionMap.SetResource(baseReflection);
            _effect.FxNormalMap.SetResource(normals);

            _effect.FxEyePosW.Set(camera.Position);
            _effect.FxWorldViewProj.SetMatrix(camera.ViewProj);
            _effect.FxWorldViewProjInv.SetMatrix(camera.ViewProjInvert);

            _effect.TechFinalStep.DrawAllPasses(holder.DeviceContext, 6);
        }
Ejemplo n.º 24
0
        protected override void DrawAfter()
        {
            base.DrawAfter();
            if (!AmbientShadowHighlight && SelectedObject == null || _outlineBuffer == null)
            {
                return;
            }

            var effect = DeviceContextHolder.GetEffect <EffectPpBasic>();

            DeviceContext.OutputMerger.BlendState = DeviceContextHolder.States.TransparentBlendState;
            DeviceContextHolder.PrepareQuad(effect.LayoutPT);
            effect.FxInputMap.SetResource(_outlineBuffer.View);

            DeviceContext.Rasterizer.State = null;
            DeviceContext.OutputMerger.DepthStencilState = DeviceContextHolder.States.DisabledDepthState;
            effect.TechCopy.DrawAllPasses(DeviceContext, 6);
        }
Ejemplo n.º 25
0
        public static byte[] ToPng(DeviceContextHolder holder, byte[] bytes, bool ignoreAlpha, 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;
                    effect.Initialize(holder.Device);

                    format = loaded.Format;
                    output.Resize(holder, loaded.Width, loaded.Height);

                    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, loaded.Width, loaded.Height, 0.0f, 1.0f));

                    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.º 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);
        }
Ejemplo 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);
        }
        private void AddShadow()
        {
            DeviceContext.OutputMerger.BlendState = _blendState;
            DeviceContext.Rasterizer.State        = null;
            DeviceContext.Rasterizer.SetViewports(Viewport);

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

            _effect.FxDepthMap.SetResource(_shadowBuffer.View);
            _effect.FxShadowViewProj.SetMatrix(_shadowDestinationTransform * _shadowCamera.ViewProj * new Matrix {
                M11 = 0.5f,
                M22 = -0.5f,
                M33 = 1.0f,
                M41 = 0.5f,
                M42 = 0.5f,
                M44 = 1.0f
            });
            _effect.TechAmbientShadow.DrawAllPasses(DeviceContext, 6);
        }
        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.º 30
0
            public void Draw(DeviceContextHolder holder, EffectSpecialTrackOutline effect, Matrix mapMatrix, MapViewData max)
            {
                if (_dirty)
                {
                    Initialize(holder);
                }

                if (AiLaneMode)
                {
                    Kn5MaterialTrackOutline.Matrix = mapMatrix;

                    if (!_materialsSet)
                    {
                        _materialsSet = true;
                        if (holder.TryToGet <IMaterialsFactory>() == null)
                        {
                            holder.Set <IMaterialsFactory>(new TrackOutlineMaterialsFactory());
                        }
                    }

                    _obj.Draw(holder, max?.GetCamera(holder) ?? GetCamera(holder), SpecialRenderMode.Simple);

                    // revert quad
                    holder.PrepareQuad(effect.LayoutPT);
                }
                else
                {
                    // local transformation matrix: global×local offset (calculated from map.ini)×local scale
                    var localScale = Matrix.Transformation2D(Vector2.Zero, 0f,
                                                             new Vector2(max.DataSize.X / DataSize.X, max.DataSize.Y / DataSize.Y) / DataScale,
                                                             Vector2.Zero, 0f, Vector2.Zero);
                    var localOffset = Matrix.AffineTransformation2D(1f, Vector2.Zero, 0f, new Vector2(
                                                                        (DataOffset.X - max.DataOffset.X) / max.DataSize.X,
                                                                        (DataOffset.Y - max.DataOffset.Y) / max.DataSize.Y));
                    effect.FxMatrix.SetMatrix(mapMatrix * localOffset * localScale);
                    effect.FxInputMap.SetResource(_view);
                    effect.TechFirstStep.DrawAllPasses(holder.DeviceContext, 6);
                }
            }
Ejemplo n.º 31
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);

            if (!(camera is CameraBase c))
            {
                return;
            }

            var near = c.NearZValue;
            var far  = c.FarZValue;

            _effect.FxAoPower.Set(aoPower);
            _effect.FxNearFar.Set(new Vector2((near - far) / (far * near), far / (far * near)));

            var valueY = (0.5f * c.FovY).Tan();
            var valueX = valueY / target.Height * target.Width;

            _effect.FxValues.Set(new Vector4(
                                     valueX,
                                     valueY,
                                     3f * aoRadiusMultiplier * valueX / target.Width,
                                     3f * aoRadiusMultiplier * valueY / target.Height * target.Width / target.Height));

            // var view = Matrix.LookAtLH(c.Position, (c.Position + c.Look), c.Up);
            // var proj = Matrix.PerspectiveFovLH(c.FovY, c.Aspect, c.NearZValue, c.FarZValue);
            // _effect.FxNormalsToViewSpace.SetMatrix(Matrix.Invert(Matrix.Transpose(proj)));
            _effect.TechHbao.DrawAllPasses(holder.DeviceContext, 6);
        }
Ejemplo n.º 32
0
        public override void Draw(DeviceContextHolder holder, ShaderResourceView depth, ShaderResourceView normals, ICamera camera, RenderTargetView target)
        {
            base.Draw(holder, depth, normals, camera, target);
            holder.PrepareQuad(_effect.LayoutPT);

            holder.DeviceContext.ClearRenderTargetView(_depths[0].TargetView, default(Color4));
            holder.DeviceContext.ClearRenderTargetView(_depths[1].TargetView, default(Color4));
            holder.DeviceContext.ClearRenderTargetView(_depths[2].TargetView, default(Color4));
            holder.DeviceContext.ClearRenderTargetView(_depths[3].TargetView, default(Color4));
            holder.DeviceContext.ClearRenderTargetView(_pingPong[0].TargetView, default(Color4));
            holder.DeviceContext.ClearRenderTargetView(_pingPong[1].TargetView, default(Color4));
            holder.DeviceContext.ClearRenderTargetView(_finalResults.TargetViews[0], default(Color4));
            holder.DeviceContext.ClearRenderTargetView(_finalResults.TargetViews[1], default(Color4));
            holder.DeviceContext.ClearRenderTargetView(_finalResults.TargetViews[2], default(Color4));
            holder.DeviceContext.ClearRenderTargetView(_finalResults.TargetViews[3], default(Color4));

            // prepare depths
            _effect.Fx_DepthSource.SetResource(depth);
            holder.DeviceContext.Rasterizer.SetViewports(new Viewport(0, 0, holder.Width / 2f, holder.Height / 2f));
            holder.DeviceContext.OutputMerger.SetTargets(_depths.Select(x => x.TargetView).ToArray());
            _effect.TechPrepareDepth.DrawAllPasses(holder.DeviceContext, 6);

            // draw stuff
            holder.DeviceContext.OutputMerger.SetTargets(target);

            /*_effect.FxDepthMap.SetResource(depth);
             * _effect.FxNormalMap.SetResource(normals);
             *
             * _effect.FxWorldViewProj.SetMatrix(camera.ViewProj);
             * _effect.FxWorldViewProjInv.SetMatrix(camera.ViewProjInvert);*/

            _effect.Fx_ViewspaceDepthSource.SetResource(_depths[0].View);
            _effect.Fx_ViewspaceDepthSource1.SetResource(_depths[1].View);
            _effect.Fx_ViewspaceDepthSource2.SetResource(_depths[2].View);
            _effect.Fx_ViewspaceDepthSource3.SetResource(_depths[3].View);
            _effect.Fx_NormalmapSource.SetResource(normals);

            // assao
            var c = camera as BaseCamera;

            if (c == null)
            {
                return;
            }

            var proj = Matrix.PerspectiveFovLH(c.FovY, c.Aspect, c.NearZValue, c.FarZValue);
            var view = Matrix.LookAtLH(c.Position.FlipX(), (c.Position + c.Look).FlipX(), c.Up.FlipX());

            //var view = Matrix.LookAtRH(c.Position, c.Position + c.Look, c.Up);
            //var view = Matrix.LookAtLH(c.Position, c.Position + c.Look, c.Up);

            //_effect.FxProjectionMatrix.Set(proj);

            _effect.FxProj.SetMatrix(proj);


            float depthLinearizeMul = -proj.M43;          // float depthLinearizeMul = ( clipFar * clipNear ) / ( clipFar - clipNear );
            float depthLinearizeAdd = proj.M22;           // float depthLinearizeAdd = clipFar / ( clipFar - clipNear );

            // correct the handedness issue. need to make sure this below is correct, but I think it is.
            if (depthLinearizeMul * depthLinearizeAdd < 0)
            {
                depthLinearizeAdd = -depthLinearizeAdd;
            }

            float tanHalfFOVY = 1.0f / proj.M22;    // = tanf( drawContext.Camera.GetYFOV( ) * 0.5f );
            float tanHalfFOVX = 1.0F / proj.M11;    // = tanHalfFOVY * drawContext.Camera.GetAspect( );

            var settings = new {
                FadeOutFrom           = 50f,
                FadeOutTo             = 300f,
                HorizonAngleThreshold = 0.06f,
                Radius               = 1.2f,
                QualityLevel         = 2,
                AdaptiveQualityLimit = 0.45f,
                Sharpness            = 0.98f,

                TemporalSupersamplingAngleOffset  = 0f,
                TemporalSupersamplingRadiusOffset = 1f,

                DetailShadowStrength = 0.5f,
            };

            var halfSize = 0.5f;

            var consts = new EffectPpAssao.ASSAOConstants {
                ViewportPixelSize     = new Vector2(1f / holder.Width, 1f / holder.Height),
                HalfViewportPixelSize = new Vector2(halfSize / (holder.Width + 1), halfSize / (holder.Height + 1)),

                Viewport2xPixelSize       = new Vector2(2f / holder.Width, 2f / holder.Height),
                Viewport2xPixelSize_x_025 = new Vector2(0.5f / holder.Width, 0.5f / holder.Height),

                DepthUnpackConsts = new Vector2(depthLinearizeMul, depthLinearizeAdd),
                CameraTanHalfFOV  = new Vector2(tanHalfFOVX, tanHalfFOVY),

                NDCToViewMul = new Vector2(tanHalfFOVX * 2.0f, tanHalfFOVY * -2.0f),
                NDCToViewAdd = new Vector2(tanHalfFOVX * -1.0f, tanHalfFOVY * 1.0f),

                EffectRadius                = 1.2f,
                EffectShadowStrength        = 1f * 4.3f,
                EffectShadowPow             = 1.5f,
                EffectShadowClamp           = 0.98f,
                EffectFadeOutMul            = -1.0f / (settings.FadeOutTo - settings.FadeOutFrom),
                EffectFadeOutAdd            = settings.FadeOutFrom / (settings.FadeOutTo - settings.FadeOutFrom) + 1.0f,
                EffectHorizonAngleThreshold = settings.HorizonAngleThreshold.Saturate(),

                DepthPrecisionOffsetMod = 0.9992f
            };

            float effectSamplingRadiusNearLimit = (settings.Radius * 1.2f);

            if (settings.QualityLevel == 0)
            {
                //consts.EffectShadowStrength     *= 0.9f;
                effectSamplingRadiusNearLimit *= 1.50f;
            }

            effectSamplingRadiusNearLimit          /= tanHalfFOVY; // to keep the effect same regardless of FOV
            consts.EffectSamplingRadiusNearLimitRec = 1.0f / effectSamplingRadiusNearLimit;
            consts.AdaptiveSampleCountLimit         = settings.AdaptiveQualityLimit;
            consts.NegRecEffectRadius = -1.0f / consts.EffectRadius;

            var pass = 0;

            consts.PerPassFullResCoordOffset = new Vector2(pass % 2, pass / 2);
            consts.PerPassFullResUVOffset    = new Vector2(((pass % 2) - 0.0f) / holder.Width, ((pass / 2) - 0.0f) / holder.Height);

            consts.InvSharpness        = MathUtils.Clamp(1.0f - settings.Sharpness, 0.0f, 1.0f);
            consts.PassIndex           = pass;
            consts.QuarterResPixelSize = new Vector2(0.25f / (float)holder.Width, 0.25f / (float)holder.Height);

            float     additionalAngleOffset = settings.TemporalSupersamplingAngleOffset;  // if using temporal supersampling approach (like "Progressive Rendering Using Multi-frame Sampling" from GPU Pro 7, etc.)
            float     additionalRadiusScale = settings.TemporalSupersamplingRadiusOffset; // if using temporal supersampling approach (like "Progressive Rendering Using Multi-frame Sampling" from GPU Pro 7, etc.)
            const int subPassCount          = 5;

            consts.PatternRotScaleMatrices = new Vector4[5];
            for (int subPass = 0; subPass < subPassCount; subPass++)
            {
                int a = pass;
                int b = subPass;

                var spmap = new int[] { 0, 1, 4, 3, 2 };
                b = spmap[subPass];

                float ca, sa;
                float angle0 = ((float)a + (float)b / (float)subPassCount) * (3.1415926535897932384626433832795f) * 0.5f;
                angle0 += additionalAngleOffset;

                ca = MathF.Cos(angle0);
                sa = MathF.Sin(angle0);

                float scale = 1.0f + (a - 1.5f + (b - (subPassCount - 1.0f) * 0.5f) / (float)subPassCount) * 0.07f;
                scale *= additionalRadiusScale;

                consts.PatternRotScaleMatrices[subPass] = new Vector4(scale * ca, scale * -sa, -scale * sa, -scale * ca);
            }

            consts.NormalsUnpackMul = 1.0f;
            consts.NormalsUnpackAdd = 0.0f;

            consts.DetailAOStrength = settings.DetailShadowStrength;
            consts.Dummy0           = 0.0f;

            //consts.NormalsWorldToViewspaceMatrix = Matrix.Transpose(view);
            consts.NormalsWorldToViewspaceMatrix = view;

            _effect.Fx_ASSAOConsts.Set(consts);


            _effect.FxProjInv.SetMatrix(Matrix.Invert(proj));
            //_effect.FxDitherScale.Set(holder.Width / 4);
            //_effect.FxRenderTargetResolution.Set(new Vector2(holder.Width, holder.Height));

            var corners = new [] {
                new Vector4(-1f, -1f, 0f, 1f),
                new Vector4(1f, -1f, 0f, 1f),
                new Vector4(1f, 1f, 0f, 1f),
                new Vector4(-1f, 1f, 0f, 1f)
            };

            var inverseProjection = Matrix.Invert(proj);

            for (var i = 0; i < 4; ++i)
            {
                corners[i]  = Vector4.Transform(corners[i], inverseProjection);
                corners[i] /= corners[i].W;
                corners[i] /= corners[i].Z;
            }

            _effect.FxViewFrustumVectors.Set(corners);
            //_effect.FxNormalsToViewSpace.SetMatrix(Matrix.Invert(Matrix.Transpose(camera.ViewProj)));
            //_effect.FxNormalsToViewSpace.SetMatrix(Matrix.Invert(Matrix.Transpose(camera.Proj)));
            _effect.FxNormalsToViewSpace.SetMatrix(camera.View);
            // _effect.Fx

            _effect.TechAssao.DrawAllPasses(holder.DeviceContext, 6);
        }
Ejemplo n.º 33
0
 public void Draw(DeviceContextHolder holder, ShaderResourceView view, RenderTargetView target) {
     holder.DeviceContext.OutputMerger.SetTargets(target);
     holder.PrepareQuad(_effect.LayoutPT);
     _effect.FxInputMap.SetResource(view);
     _effect.TechCopy.DrawAllPasses(holder.DeviceContext, 6);
 }
Ejemplo n.º 34
0
 public void Final(DeviceContextHolder holder, ShaderResourceView view, RenderTargetView target, bool checkedBackground) {
     holder.DeviceContext.OutputMerger.SetTargets(target);
     holder.PrepareQuad(_effect.LayoutPT);
     _effect.FxInputMap.SetResource(view);
     (checkedBackground ? _effect.TechFinalCheckers : _effect.TechFinal).DrawAllPasses(holder.DeviceContext, 6);
 }
Ejemplo n.º 35
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);*/
        }