Example #1
0
        /// <inheritdoc />
        public override void Render(GPUContext context, SceneRenderTask task, GPUTexture input, GPUTexture output)
        {
            if (Viewport == null)
            {
                throw new NullReferenceException();
            }

            Profiler.BeginEventGPU("Editor Primitives");

            // Check if use MSAA
            var format = output.Format;

            GPUDevice.GetFeatures(format, out var formatSupport);
            bool enableMsaa = formatSupport.MSAALevelMax >= MSAALevel.X4 && Editor.Instance.Options.Options.Visual.EnableMSAAForDebugDraw;

            // Prepare
            var msaaLevel = enableMsaa ? MSAALevel.X4 : MSAALevel.None;
            var width     = output.Width;
            var height    = output.Height;
            var desc      = GPUTextureDescription.New2D(width, height, format, GPUTextureFlags.RenderTarget | GPUTextureFlags.ShaderResource, 1, 1, msaaLevel);
            var target    = RenderTargetPool.Get(ref desc);

            desc = GPUTextureDescription.New2D(width, height, PixelFormat.D24_UNorm_S8_UInt, GPUTextureFlags.DepthStencil, 1, 1, msaaLevel);
            var targetDepth = RenderTargetPool.Get(ref desc);

            // Copy frame and clear depth
            context.Draw(target, input);
            context.ClearDepth(targetDepth);

            // Draw gizmos and other editor primitives (collect draw calls only)
            _drawCallsCollector.Clear();
            for (int i = 0; i < Viewport.Gizmos.Count; i++)
            {
                Viewport.Gizmos[i].Draw(_drawCallsCollector);
            }
            Viewport.DrawEditorPrimitives(context, task, target, targetDepth, _drawCallsCollector);

            // Draw gizmos (actual drawing)
            _drawCallsCollector.ExecuteDrawCalls(context, task, target, DrawPass.GBuffer);
            _drawCallsCollector.ExecuteDrawCalls(context, task, target, DrawPass.Forward);

            // Resolve MSAA texture
            if (enableMsaa)
            {
                context.ResolveMultisample(target, output);
            }
            else
            {
                context.Draw(output, target);
            }

            // Cleanup
            RenderTargetPool.Release(targetDepth);
            RenderTargetPool.Release(target);

            Profiler.EndEventGPU();
        }
Example #2
0
        public override void Draw(DrawingContext context, IList <IDrawableObject> drawables)
        {
            if (Material == null)
            {
                Material = adoptionMaterial = new AdaptionMaterial(context.graphics);
            }
            else if (Material != adoptionMaterial)
            {
                throw new InvalidOperationException();
            }

            bool needLocalTexture = false;

            if (needLocalTexture = (currentFrame == null))
            {
                PrepareRenderTarget(context.graphics, InputTexture, null);
                currentFrame.Begin();
            }

            // Disable the adoption effect when we don't have a valid last frame texture
            // or when the adoption effect has been suspended for several frames.
#if SILVERLIGHT
            if (lastFrame == null || lastFrame.IsDisposed)
#else
            if (lastFrame == null || lastFrame.IsDisposed || lastFrame.IsContentLost)
#endif
            {
                CopyToScreen(context, drawables);
            }
            else
            {
                var graphics = context.graphics;
                graphics.Textures[0]      = adoptionMaterial.texture;
                graphics.Textures[1]      = lastFrame;
                graphics.SamplerStates[0] = graphics.SamplerStates[1] = SamplerState.PointClamp;
                adoptionMaterial.effect.Delta.SetValue((1 - (float)Math.Pow(0.98f, 30 * context.elapsedTime)) * Speed);

                base.Draw(context, drawables);

                graphics.SamplerStates[1] = context.SamplerState;
            }

            if (needLocalTexture)
            {
                currentFrame.End();

                InputTexture = currentFrame;
                CopyToScreen(context, drawables);
            }

            RenderTargetPool.Unlock(lastFrame);
            lastFrame    = currentFrame;
            currentFrame = null;
        }
        public void ValidateDuplicateRecycleCube()
        {
            GlobalSettings.ValidationLevel = 0;
            var p  = new RenderTargetPool(_graphicsService);
            var r0 = p.ObtainCube(new RenderTargetFormat(32, 32, false, SurfaceFormat.Color, DepthFormat.None));

            p.Recycle(r0);
            p.Recycle(r0);

            GlobalSettings.ValidationLevel = 0xff;
            p = new RenderTargetPool(_graphicsService);
            p.Recycle(r0);
            Assert.Throws <InvalidOperationException>(() => p.Recycle(r0));
        }
        public void Clear()
        {
            var p = new RenderTargetPool(_graphicsService);
              var r0 = p.Obtain2D(new RenderTargetFormat(100, 100, false, SurfaceFormat.Color, DepthFormat.None));
              var r1 = p.Obtain2D(new RenderTargetFormat(100, 100, false, SurfaceFormat.Color, DepthFormat.None));

              p.Recycle(r0);
              p.Recycle(r1);

              //Assert.IsFalse(r0.IsDisposed);      // Not disposing RT anymore because of XNA bug.
              //Assert.IsFalse(r1.IsDisposed);

              p.Clear();

              //Assert.IsTrue(r0.IsDisposed);
              //Assert.IsTrue(r1.IsDisposed);
              Assert.AreEqual(0, p.RenderTargets2D.Count);
              Assert.AreEqual(0, p.Counters2D.Count);
        }
        public void FrameLimit()
        {
            var p = new RenderTargetPool(_graphicsService);
              p.FrameLimit = 5;

              var r0 = p.Obtain2D(new RenderTargetFormat(100, 100, false, SurfaceFormat.Color, DepthFormat.None));
              var r1 = p.Obtain2D(new RenderTargetFormat(100, 100, false, SurfaceFormat.Color, DepthFormat.None));

              Assert.IsFalse(r0.IsDisposed);
              Assert.IsFalse(r1.IsDisposed);

              p.Recycle(r0);

              p.Update();
              p.Update();

              p.Recycle(r1);

              p.Update();
              p.Update();

              Assert.IsFalse(r0.IsDisposed);
              Assert.IsFalse(r1.IsDisposed);
              Assert.AreEqual(2, p.Counters2D.Count);

              p.Update();
              p.Update();

              Assert.IsTrue(r0.IsDisposed);
              Assert.IsFalse(r1.IsDisposed);
              Assert.AreEqual(1, p.Counters2D.Count);

              p.Update();
              p.Update();

              Assert.IsTrue(r0.IsDisposed);
              Assert.IsTrue(r1.IsDisposed);
              Assert.AreEqual(0, p.Counters2D.Count);
        }
        public void ObtainRecycle()
        {
            var p = new RenderTargetPool(_graphicsService);

            p.FrameLimit = 5;

            var r0 = p.Obtain2D(new RenderTargetFormat(32, 32, false, SurfaceFormat.Color, DepthFormat.None));
            var r1 = p.Obtain2D(new RenderTargetFormat(32, 64, false, SurfaceFormat.Color, DepthFormat.None));
            var r2 = p.Obtain2D(new RenderTargetFormat(32, 32, true, SurfaceFormat.Color, DepthFormat.None));
            var r3 = p.Obtain2D(new RenderTargetFormat(32, 32, false, SurfaceFormat.Single, DepthFormat.None));
            var r4 = p.Obtain2D(new RenderTargetFormat(32, 32, false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8));

            p.Recycle(r0);
            p.Recycle(r1);
            p.Recycle(r2);
            p.Recycle(r3);
            p.Recycle(r4);

            Assert.AreEqual(r0, p.Obtain2D(new RenderTargetFormat(32, 32, false, SurfaceFormat.Color, DepthFormat.None)));
            Assert.AreNotEqual(r0, p.Obtain2D(new RenderTargetFormat(32, 32, false, SurfaceFormat.Color, DepthFormat.None)));
            p.Recycle(r0);

            Assert.AreEqual(r2, p.Obtain2D(new RenderTargetFormat(32, 32, true, SurfaceFormat.Color, DepthFormat.None)));
            Assert.AreNotEqual(r2, p.Obtain2D(new RenderTargetFormat(32, 32, true, SurfaceFormat.Color, DepthFormat.None)));
            p.Recycle(r2);

            Assert.AreEqual(r3, p.Obtain2D(new RenderTargetFormat(32, 32, false, SurfaceFormat.Single, DepthFormat.None)));
            Assert.AreNotEqual(r3, p.Obtain2D(new RenderTargetFormat(32, 32, false, SurfaceFormat.Single, DepthFormat.None)));
            p.Recycle(r3);

            Assert.AreEqual(r1, p.Obtain2D(new RenderTargetFormat(32, 64, false, SurfaceFormat.Color, DepthFormat.None)));
            Assert.AreNotEqual(r1, p.Obtain2D(new RenderTargetFormat(32, 64, false, SurfaceFormat.Color, DepthFormat.None)));
            p.Recycle(r1);

            Assert.AreEqual(r4, p.Obtain2D(new RenderTargetFormat(32, 32, false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8)));
            Assert.AreNotEqual(r4, p.Obtain2D(new RenderTargetFormat(32, 32, false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8)));
            p.Recycle(r4);
        }
        public void ObtainRecycle()
        {
            var p = new RenderTargetPool(_graphicsService);
              p.FrameLimit = 5;

              var r0 = p.Obtain2D(new RenderTargetFormat(32, 32, false, SurfaceFormat.Color, DepthFormat.None));
              var r1 = p.Obtain2D(new RenderTargetFormat(32, 64, false, SurfaceFormat.Color, DepthFormat.None));
              var r2 = p.Obtain2D(new RenderTargetFormat(32, 32, true, SurfaceFormat.Color, DepthFormat.None));
              var r3 = p.Obtain2D(new RenderTargetFormat(32, 32, false, SurfaceFormat.Single, DepthFormat.None));
              var r4 = p.Obtain2D(new RenderTargetFormat(32, 32, false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8));

              p.Recycle(r0);
              p.Recycle(r1);
              p.Recycle(r2);
              p.Recycle(r3);
              p.Recycle(r4);

              Assert.AreEqual(r0, p.Obtain2D(new RenderTargetFormat(32, 32, false, SurfaceFormat.Color, DepthFormat.None)));
              Assert.AreNotEqual(r0, p.Obtain2D(new RenderTargetFormat(32, 32, false, SurfaceFormat.Color, DepthFormat.None)));
              p.Recycle(r0);

              Assert.AreEqual(r2, p.Obtain2D(new RenderTargetFormat(32, 32, true, SurfaceFormat.Color, DepthFormat.None)));
              Assert.AreNotEqual(r2, p.Obtain2D(new RenderTargetFormat(32, 32, true, SurfaceFormat.Color, DepthFormat.None)));
              p.Recycle(r2);

              Assert.AreEqual(r3, p.Obtain2D(new RenderTargetFormat(32, 32, false, SurfaceFormat.Single, DepthFormat.None)));
              Assert.AreNotEqual(r3, p.Obtain2D(new RenderTargetFormat(32, 32, false, SurfaceFormat.Single, DepthFormat.None)));
              p.Recycle(r3);

              Assert.AreEqual(r1, p.Obtain2D(new RenderTargetFormat(32, 64, false, SurfaceFormat.Color, DepthFormat.None)));
              Assert.AreNotEqual(r1, p.Obtain2D(new RenderTargetFormat(32, 64, false, SurfaceFormat.Color, DepthFormat.None)));
              p.Recycle(r1);

              Assert.AreEqual(r4, p.Obtain2D(new RenderTargetFormat(32, 32, false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8)));
              Assert.AreNotEqual(r4, p.Obtain2D(new RenderTargetFormat(32, 32, false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8)));
              p.Recycle(r4);
        }
        public void ValidateDuplicateRecycleCube()
        {
            GlobalSettings.ValidationLevel = 0;
              var p = new RenderTargetPool(_graphicsService);
              var r0 = p.ObtainCube(new RenderTargetFormat(32, 32, false, SurfaceFormat.Color, DepthFormat.None));
              p.Recycle(r0);
              p.Recycle(r0);

              GlobalSettings.ValidationLevel = 0xff;
              p = new RenderTargetPool(_graphicsService);
              p.Recycle(r0);
              Assert.Throws<InvalidOperationException>(() => p.Recycle(r0));
        }
 public void Properties()
 {
     var p = new RenderTargetPool(_graphicsService);
       Assert.AreEqual(p.GraphicsService, _graphicsService);
       Assert.IsTrue(p.FrameLimit > 0);
 }
Example #10
0
 public override RenderTarget2D PrepareRenderTarget(GraphicsDevice graphics, Texture2D input, SurfaceFormat?preferredFormat)
 {
     currentFrame = base.PrepareRenderTarget(graphics, input, preferredFormat);
     RenderTargetPool.Lock(currentFrame);
     return(currentFrame);
 }
Example #11
0
    public GraphicsManager(GraphicsDevice graphicsDevice, GameWindow gameWindow, ContentManager content)
    {
      if (graphicsDevice == null)
        throw new ArgumentNullException("graphicsDevice");
      if (content == null)
        throw new ArgumentNullException("content");

      GraphicsDevice = graphicsDevice;
      graphicsDevice.DeviceResetting += OnGraphicsDeviceResetting;
      //graphicsDevice.DeviceReset += OnGraphicsDeviceReset;
      GraphicsDevice.Disposing += OnGraphicsDeviceDisposing;

      Content = content;

      RenderTargetPool = new RenderTargetPool(this);
      Screens = new GraphicsScreenCollection();

      if (gameWindow != null)
        GameForm = PlatformHelper.GetForm(gameWindow.Handle);

      PresentationTargets = new PresentationTargetCollection();
      PresentationTargets.CollectionChanged += OnPresentationTargetsChanged;

      EffectInterpreters = new EffectInterpreterCollection
      {
        new StockEffectInterpreter(),
        new DefaultEffectInterpreter(),
        new SceneEffectInterpreter(),
#if !WINDOWS_PHONE && !XBOX360
        new TerrainEffectInterpreter(),
#endif
        new Dxsas08EffectInterpreter(),
      };
      EffectBinders = new EffectBinderCollection
      {
        new StockEffectBinder(),
        new DefaultEffectBinder(this),
        new SceneEffectBinder(),
#if !WINDOWS_PHONE && !XBOX360
        new TerrainEffectBinder(),
#endif
      };

      Data = new Dictionary<string, object>();
      Frame = -1;
      ShapeMeshCache = new ShapeMeshCache(this);
    }