示例#1
0
        public CreateTargetComponent(RenderTargetInfo targetInfo, string resourceName = null)
        {
            this.targetInfo = targetInfo;

            counter   = (counter + 1) % (int.MaxValue - 1);
            this.name = resourceName ?? string.Format("anonymous-{0}-{1}", counter, targetInfo.GetHashCode());
        }
示例#2
0
        /// <summary>
        /// Informs the renderer of a resource this component is generating.
        /// </summary>
        /// <param name="name">The name of the resource.</param>
        /// <param name="isLeftSet"><c>true</c> if this component will leave the render target set on the device; else <c>false</c>.</param>
        /// <param name="finaliser">A cleanup method, called when the resource is no longer needed. <c>null</c> for the default finaliser.</param>
        /// <param name="width">The width of the render target.</param>
        /// <param name="height">The height of the render target.</param>
        /// <param name="surfaceFormat">The surface format of the render target.</param>
        /// <param name="depthFormat">The depth format of the render target.</param>
        /// <param name="multiSampleCount">The multi sample count of the render target.</param>
        /// <param name="mipMap">The number of mip map levels of the render target.</param>
        /// <param name="usage">The render target usage of the render target.</param>
        public void DefineOutput(
            string name,
            bool isLeftSet = true,
            Action <Renderer, RenderTarget2D> finaliser = null,
            int width  = 0,
            int height = 0,
            SurfaceFormat surfaceFormat = SurfaceFormat.Color,
            DepthFormat depthFormat     = DepthFormat.None,
            int multiSampleCount        = 0,
            bool mipMap             = false,
            RenderTargetUsage usage = RenderTargetUsage.DiscardContents)
        {
            var info = new RenderTargetInfo()
            {
                Width            = width,
                Height           = height,
                SurfaceFormat    = surfaceFormat,
                DepthFormat      = depthFormat,
                MultiSampleCount = multiSampleCount,
                MipMap           = mipMap,
                Usage            = usage
            };

            DefineOutput(name, isLeftSet, finaliser, info);
        }
示例#3
0
        public CreateTargetComponent(RenderTargetInfo targetInfo, string resourceName = null)
        {
            _targetInfo = targetInfo;

            _counter = (_counter + 1) % (int.MaxValue - 1);
            _name = resourceName ?? string.Format("anonymous-{0}-{1}", _counter, targetInfo.GetHashCode());
        }
示例#4
0
 public bool Equals(RenderTargetInfo info)
 {
     return(this.Height == info.Height &&
            this.Width == info.Width &&
            this.SurfaceFormat == info.SurfaceFormat &&
            this.DepthFormat == info.DepthFormat &&
            this.MultiSampleCount == info.MultiSampleCount &&
            this.MipMap == info.MipMap &&
            this.Usage == info.Usage);
 }
示例#5
0
 public bool Equals(RenderTargetInfo info)
 {
     return Height == info.Height
         && Width == info.Width
         && SurfaceFormat == info.SurfaceFormat
         && DepthFormat == info.DepthFormat
         && MultiSampleCount == info.MultiSampleCount
         && MipMap == info.MipMap
         && Usage == info.Usage;
 }
示例#6
0
        public static RenderTarget2D GetTarget(GraphicsDevice device, RenderTargetInfo info, string name = null)
        {
            RenderTargetInfo mapped;
            bool             wasMapped = infoMappings.TryGetValue(info, out mapped);

            if (!wasMapped)
            {
                mapped = info;
            }

            var stack = GetPool(mapped);

            if (stack.Count > 0)
            {
                var t = stack.Pop();
                t.Tag = name;
#if DEBUG
                active.Add(t.Tag as string);
#endif
                return(t);
            }

            var target = new RenderTarget2D(device, mapped.Width, mapped.Height, mapped.MipMap, mapped.SurfaceFormat, mapped.DepthFormat, mapped.MultiSampleCount, mapped.Usage);
            target.Tag = name;

            if (!wasMapped)
            {
                var targetInfo = RenderTargetInfo.FromRenderTarget(target);
                infoMappings[info] = targetInfo;
            }

#if PROFILE
            numRenderTargets.Value++;

            var   resolution = target.Width * target.Height;
            float size       = resolution * target.Format.FormatSize();
            if (target.MultiSampleCount > 0)
            {
                size *= target.MultiSampleCount;
            }
            if (info.MipMap)
            {
                size *= 1.33f;
            }
            size += resolution * target.DepthStencilFormat.FormatSize();
            renderTargetMemory.Value += size / (1024 * 1024);
#endif

#if DEBUG
            active.Add(target.Tag as string);
#endif

            return(target);
        }
示例#7
0
        public override void Draw(Renderer renderer)
        {
            var resolution = renderer.Data.Get<Vector2>("resolution").Value;
            var targetInfo = new RenderTargetInfo((int) resolution.X, (int) resolution.Y, SurfaceFormat.Color, DepthFormat.None, 4, default(bool), default(RenderTargetUsage));

            var target = RenderTargetManager.GetTarget(renderer.Device, targetInfo);
            renderer.Device.SetRenderTarget(target);
            renderer.Device.Clear(Colour);

            Output("scene", target);
        }
示例#8
0
        private static Stack <RenderTarget2D> GetPool(RenderTargetInfo info)
        {
            Stack <RenderTarget2D> stack;

            if (!pool.TryGetValue(info, out stack))
            {
                stack = new Stack <RenderTarget2D>();
                pool.Add(info, stack);
            }

            return(stack);
        }
示例#9
0
        public static RenderTarget2D GetTarget(GraphicsDevice device, int width, int height, SurfaceFormat surfaceFormat = SurfaceFormat.Color, DepthFormat depthFormat = DepthFormat.None, int multiSampleCount = 0, bool mipMap = false, RenderTargetUsage usage = RenderTargetUsage.DiscardContents, string name = null)
        {
            var info = new RenderTargetInfo()
            {
                Width            = width,
                Height           = height,
                SurfaceFormat    = surfaceFormat,
                DepthFormat      = depthFormat,
                MultiSampleCount = multiSampleCount,
                MipMap           = mipMap,
                Usage            = usage
            };

            return(GetTarget(device, info, name));
        }
示例#10
0
        /// <summary>
        /// Informs the renderer of a resource this component is generating.
        /// </summary>
        /// <param name="name">The name of the resource.</param>
        /// <param name="isLeftSet"><c>true</c> if this component will leave the render target set on the device; else <c>false</c>.</param>
        /// <param name="finaliser">A cleanup method, called when the resource is no longer needed. <c>null</c> for the default finaliser.</param>
        /// <param name="format">The format of the render target.</param>
        public void DefineOutput(
            string name,
            bool isLeftSet,
            Action <Renderer, RenderTarget2D> finaliser,
            RenderTargetInfo format)
        {
            var resource = new Resource()
            {
                Name      = name,
                Finaliser = finaliser ?? Resource.DefaultFinaliser,
                IsLeftSet = isLeftSet,
                Format    = format
            };

            outputs.Add(resource);
        }
示例#11
0
        public static void RecycleTarget(RenderTarget2D target)
        {
            var info = RenderTargetInfo.FromRenderTarget(target);

#if DEBUG
            if (GetPool(info).Contains(target))
            {
                throw new InvalidOperationException("Render target has already been freed.");
            }

            active.Remove(target.Tag as string);
#endif

            GetPool(info).Push(target);
            target.Tag = null;
        }
示例#12
0
        public override void Draw(Renderer renderer)
        {
            var resolution = renderer.Data.Get<Vector2>("resolution").Value;
            var targetInfo = new RenderTargetInfo()
            {
                Width = (int)resolution.X,
                Height = (int)resolution.Y,
                SurfaceFormat = SurfaceFormat.Color,
                DepthFormat = DepthFormat.None,
                MultiSampleCount = 4
            };

            var target = RenderTargetManager.GetTarget(renderer.Device, targetInfo);
            renderer.Device.SetRenderTarget(target);
            renderer.Device.Clear(Colour);

            Output("scene", target);
        }
示例#13
0
        public override void Draw(Renderer renderer)
        {
            var info = _targetInfo;
            if (info.Width == 0 || info.Height == 0)
            {
                var resolution = renderer.Data.GetValue(new TypedName<Vector2>("resolution"));
                info = new RenderTargetInfo(
                    (int) resolution.X,
                    (int) resolution.Y,
                    info.SurfaceFormat,
                    info.DepthFormat,
                    info.MultiSampleCount,
                    info.MipMap,
                    info.Usage
                );
            }

            var target = RenderTargetManager.GetTarget(renderer.Device, info);
            renderer.Device.SetRenderTarget(target);
            renderer.Device.Clear(ClearOptions.Target | ClearOptions.DepthBuffer | ClearOptions.Stencil, Color.Black, 1, 0);

            Output(_name, target);
        }
示例#14
0
        /// <summary>
        /// Informs the renderer of a resource this component is generating.
        /// </summary>
        /// <param name="name">The name of the resource.</param>
        /// <param name="isLeftSet"><c>true</c> if this component will leave the render target set on the device; else <c>false</c>.</param>
        /// <param name="finaliser">A cleanup method, called when the resource is no longer needed. <c>null</c> for the default finaliser.</param>
        /// <param name="width">The width of the render target.</param>
        /// <param name="height">The height of the render target.</param>
        /// <param name="surfaceFormat">The surface format of the render target.</param>
        /// <param name="depthFormat">The depth format of the render target.</param>
        /// <param name="multiSampleCount">The multi sample count of the render target.</param>
        /// <param name="mipMap">The number of mip map levels of the render target.</param>
        /// <param name="usage">The render target usage of the render target.</param>
        public void DefineOutput(
            string name,
            bool isLeftSet = true,
            Action<Renderer, RenderTarget2D> finaliser = null,
            int width = 0,
            int height = 0,
            SurfaceFormat surfaceFormat = SurfaceFormat.Color,
            DepthFormat depthFormat = DepthFormat.None,
            int multiSampleCount = 0,
            bool mipMap = false,
            RenderTargetUsage usage = RenderTargetUsage.DiscardContents)
        {
            var info = new RenderTargetInfo()
            {
                Width = width,
                Height = height,
                SurfaceFormat = surfaceFormat,
                DepthFormat = depthFormat,
                MultiSampleCount = multiSampleCount,
                MipMap = mipMap,
                Usage = usage
            };

            DefineOutput(name, isLeftSet, finaliser, info);
        }
示例#15
0
        public static RenderTarget2D GetTarget(GraphicsDevice device, int width, int height, SurfaceFormat surfaceFormat = SurfaceFormat.Color, DepthFormat depthFormat = DepthFormat.None, int multiSampleCount = 0, bool mipMap = false, RenderTargetUsage usage = RenderTargetUsage.PreserveContents, string name = null)
        {
            var info = new RenderTargetInfo(width, height, surfaceFormat, depthFormat, multiSampleCount, mipMap, usage);

            return GetTarget(device, info, name);
        }
示例#16
0
        public static RenderTarget2D GetTarget(GraphicsDevice device, RenderTargetInfo info, string name = null)
        {
            RenderTargetInfo mapped;
            bool wasMapped = _infoMappings.TryGetValue(info, out mapped);
            if (!wasMapped)
                mapped = info;

            var stack = GetPool(mapped);
            if (stack.Count > 0)
            {
                var t = stack.Pop();
                t.Tag = name;
#if DEBUG
                _active.Add(t.Tag as string);
#endif
                return t;
            }

            var target = new RenderTarget2D(device, mapped.Width, mapped.Height, mapped.MipMap, mapped.SurfaceFormat, mapped.DepthFormat, mapped.MultiSampleCount, mapped.Usage) { Tag = name };

            if (!wasMapped)
            {
                var targetInfo = RenderTargetInfo.FromRenderTarget(target);
                _infoMappings[info] = targetInfo;
            }

#if PROFILE
            _numRenderTargets.Add(1);

            var resolution = target.Width * target.Height;
            float size = resolution * target.Format.FormatSize();
            if (target.MultiSampleCount > 0)
                size *= target.MultiSampleCount;
            if (info.MipMap)
                size *= 1.33f;
            size += resolution * target.DepthStencilFormat.FormatSize();
            _renderTargetMemory.Add(size / (1024 * 1024));
#endif

#if DEBUG
            _active.Add(target.Tag as string);
#endif

            return target;
        }
示例#17
0
            public override void Draw(Renderer renderer)
            {
                var resolution = renderer.Data.Get<Vector2>("resolution").Value;
                var targetInfo = new RenderTargetInfo() { Width = (int)resolution.X, Height = (int)resolution.Y };
                var target = RenderTargetManager.GetTarget(renderer.Device, targetInfo);
                renderer.Device.SetRenderTarget(target);

                batch.Begin();
                batch.DrawString(Font, "This is being drawn by a RenderPhase!", new Vector2(640, 360), Color.White);
                batch.End();

                Output("scene", target);
            }
示例#18
0
            public override void Draw(Renderer renderer)
            {
                var metadata = renderer.Data;
                var resolution = metadata.GetOrCreate<Vector2>(Names.View.Resolution).Value;
                var targetInfo = new RenderTargetInfo((int) resolution.X, (int) resolution.Y, default(SurfaceFormat), default(DepthFormat), default(int), default(bool), default(RenderTargetUsage));
                var target = RenderTargetManager.GetTarget(renderer.Device, targetInfo);
                renderer.Device.SetRenderTarget(target);

                var depth = metadata.GetValue<Texture2D>(Names.Deferred.Textures.Depth);
                var normals = metadata.GetValue<Texture2D>(Names.Deferred.Textures.Normals);
                var diffuse = metadata.GetValue<Texture2D>(Names.Deferred.Textures.Diffuse);

                //Save(depth, "depth.jpg");
                //Save(normals, "normal.jpg");
                //Save(diffuse, "diffuse.jpg");

                var halfWidth = (int)(resolution.X / 2);
                var halfHeight = (int)(resolution.Y / 2);

                _batch.GraphicsDevice.Clear(Color.Black);
                _batch.Begin(SpriteSortMode.Immediate, BlendState.Opaque);

                _batch.GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
                _batch.Draw(depth, new Rectangle(0, 0, halfWidth, halfHeight), Color.White);
                _batch.GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp;

                _batch.Draw(normals, new Rectangle(halfWidth, 0, halfWidth, halfHeight), Color.White);
                _batch.Draw(diffuse, new Rectangle(0, halfHeight, halfWidth, halfHeight), Color.White);
                _batch.End();

                Output("scene", target);
            }
示例#19
0
            public override void Draw(Renderer renderer)
            {
                var metadata = renderer.Data;
                var resolution = renderer.Data.Get<Vector2>("resolution").Value;
                var targetInfo = new RenderTargetInfo((int)resolution.X, (int)resolution.Y, SurfaceFormat.Rgba64, DepthFormat.Depth24Stencil8, default(int), default(bool), default(RenderTargetUsage));
                var target = RenderTargetManager.GetTarget(renderer.Device, targetInfo);
                renderer.Device.SetRenderTarget(target);

                var light = metadata.Get<Texture2D>("tonemapped").Value;
                var luminance = metadata.Get<Texture2D>("luminancemap").Value;

                //using (var stream = File.Create("luminance.jpg"))
                //    light.SaveAsJpeg(stream, light.Width, light.Height);

                var width = (int)resolution.X;
                var height = (int)resolution.Y;

                batch.GraphicsDevice.Clear(Color.Black);
                batch.Begin(SpriteSortMode.Immediate, BlendState.Opaque);

                if (drawScene)
                {
                    batch.GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
                    batch.Draw(light, new Rectangle(0, 0, width, height), Color.White);
                    //batch.Draw(luminance, new Rectangle(50, height - (height / 5) - 50, height / 5, height / 5), Color.White);
                    //batch.Draw(toneMap.AdaptedLuminance, new Rectangle(50 + 20 + (height / 5), height - (height / 5) - 50, height / 5, height / 5), Color.White);
                    batch.GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp;
                }
                else
                {
                    batch.GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
                    batch.Draw(luminance, new Rectangle(0, 0, width, height), Color.White);
                    batch.GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp;
                }

                batch.End();

                Output("scene", target);
            }
示例#20
0
        /// <summary>
        /// Informs the renderer of a resource this component is generating.
        /// </summary>
        /// <param name="name">The name of the resource.</param>
        /// <param name="isLeftSet"><c>true</c> if this component will leave the render target set on the device; else <c>false</c>.</param>
        /// <param name="finaliser">A cleanup method, called when the resource is no longer needed. <c>null</c> for the default finaliser.</param>
        /// <param name="format">The format of the render target.</param>
        public void DefineOutput(
            string name,
            bool isLeftSet,
            Action<Renderer, RenderTarget2D> finaliser,
            RenderTargetInfo format)
        {
            var resource = new Resource()
            {
                Name = name,
                Finaliser = finaliser ?? Resource.DefaultFinaliser,
                IsLeftSet = isLeftSet,
                Format = format
            };

            outputs.Add(resource);
        }
示例#21
0
        private static Stack<RenderTarget2D> GetPool(RenderTargetInfo info)
        {
            Stack<RenderTarget2D> stack;
            if (!_pool.TryGetValue(info, out stack))
            {
                stack = new Stack<RenderTarget2D>();
                _pool.Add(info, stack);
            }

            return stack;
        }
示例#22
0
            public override void Draw(Renderer renderer)
            {
                KeyboardState keyboard = Keyboard.GetState();
                if (keyboard.IsKeyDown(Keys.Space) && previousKeyboard.IsKeyUp(Keys.Space))
                    drawGBuffer = !drawGBuffer;
                previousKeyboard = keyboard;

                var metadata = renderer.Data;
                var resolution = renderer.Data.Get<Vector2>("resolution").Value;
                var targetInfo = new RenderTargetInfo((int)resolution.X, (int)resolution.Y, SurfaceFormat.Rgba64, default(DepthFormat), default(int), default(bool), default(RenderTargetUsage));
                var target = RenderTargetManager.GetTarget(renderer.Device, targetInfo);
                renderer.Device.SetRenderTarget(target);

                var depth = metadata.Get<Texture2D>("gbuffer_depth").Value;
                var normals = metadata.Get<Texture2D>("gbuffer_normals").Value;
                var diffuse = metadata.Get<Texture2D>("gbuffer_diffuse").Value;
                var light = metadata.Get<Texture2D>("lightbuffer").Value;

                //using (var stream = File.Create("lightbuffer.jpg"))
                //    light.SaveAsJpeg(stream, light.Width, light.Height);

                var halfWidth = (int)(resolution.X / 2);
                var halfHeight = (int)(resolution.Y / 2);

                batch.GraphicsDevice.Clear(Color.Black);
                batch.Begin(SpriteSortMode.Immediate, BlendState.Opaque);

                if (drawGBuffer)
                {
                    batch.GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
                    batch.Draw(depth, new Rectangle(0, 0, halfWidth, halfHeight), Color.White);
                    batch.Draw(light, new Rectangle(halfWidth, halfHeight, halfWidth, halfHeight), Color.White);
                    batch.GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp;

                    batch.Draw(normals, new Rectangle(halfWidth, 0, halfWidth, halfHeight), Color.White);
                    batch.Draw(diffuse, new Rectangle(0, halfHeight, halfWidth, halfHeight), Color.White);
                }
                else
                {
                    batch.GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp;
                    batch.Draw(light, new Rectangle(0, 0, (int)resolution.X, (int)resolution.Y), Color.White);
                    batch.GraphicsDevice.SamplerStates[0] = SamplerState.LinearClamp;
                }

                batch.End();

                Output("scene", target);
            }
示例#23
0
            public override void Draw(Renderer renderer)
            {
                var resolution = renderer.Data.Get<Vector2>("resolution").Value;
                var targetInfo = new RenderTargetInfo((int) resolution.X, (int) resolution.Y, default(SurfaceFormat), default(DepthFormat), default(int), default(bool), default(RenderTargetUsage));
                var target = RenderTargetManager.GetTarget(renderer.Device, targetInfo);
                renderer.Device.SetRenderTarget(target);

                _batch.Begin();
                _batch.DrawString(Font, "This is being drawn by a RenderPhase!", new Vector2(640, 360).ToXNA(), Color.White);
                _batch.End();

                Output("scene", target);
            }
示例#24
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ResourceInfo"/> struct.
 /// </summary>
 /// <param name="name">The name.</param>
 /// <param name="format">The format.</param>
 public ResourceInfo(string name, RenderTargetInfo format)
     : this()
 {
     this.Name = name;
     this.Format = format;
 }
示例#25
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ResourceInfo"/> struct.
 /// </summary>
 /// <param name="name">The name.</param>
 /// <param name="format">The format.</param>
 public ResourceInfo(string name, RenderTargetInfo format)
     : this()
 {
     this.Name   = name;
     this.Format = format;
 }