Inheritance: GraphicsResource
Esempio n. 1
0
        /// <summary>
        /// Creates texture
        /// </summary>
        /// <param name="device"></param>
        public TextureCube(GraphicsDevice device, int size, ColorFormat format, bool mips, bool srgb = false) : base(device)
        {
            this.Width    = size;
            this.Depth    = 1;
            this.Height   = size;
            this.format   = format;
            this.mipCount = mips ? ShaderResource.CalculateMipLevels(Width, Height) : 1;

            //Log.Warning("CHECK ARRAY SIZE!");

            var texDesc = new Texture2DDescription();

            texDesc.ArraySize                 = 6;
            texDesc.BindFlags                 = BindFlags.ShaderResource;
            texDesc.CpuAccessFlags            = CpuAccessFlags.None;
            texDesc.Format                    = MakeTypeless(Converter.Convert(format));
            texDesc.Height                    = Height;
            texDesc.MipLevels                 = mipCount;
            texDesc.OptionFlags               = ResourceOptionFlags.TextureCube;
            texDesc.SampleDescription.Count   = 1;
            texDesc.SampleDescription.Quality = 0;
            texDesc.Usage = ResourceUsage.Default;
            texDesc.Width = Width;

            texCube = new D3D.Texture2D(device.Device, texDesc);
            SRV     = new ShaderResourceView(device.Device, texCube);
        }
Esempio n. 2
0
        /// <summary>
        /// Creates texture
        /// </summary>
        /// <param name="device"></param>
        public VolumeRWTexture(GraphicsDevice device, int width, int height, int depth, ColorFormat format, bool mips) : base(device)
        {
            this.Width    = width;
            this.Height   = height;
            this.Depth    = depth;
            this.format   = format;
            this.mipCount = mips ? ShaderResource.CalculateMipLevels(Width, Height, Depth) : 1;

            var texDesc = new Texture3DDescription();

            texDesc.BindFlags      = BindFlags.ShaderResource | BindFlags.UnorderedAccess;
            texDesc.CpuAccessFlags = CpuAccessFlags.None;
            texDesc.Format         = Converter.Convert(format);
            texDesc.Height         = Height;
            texDesc.MipLevels      = mipCount;
            texDesc.OptionFlags    = ResourceOptionFlags.None;
            texDesc.Usage          = ResourceUsage.Default;
            texDesc.Width          = Width;
            texDesc.Depth          = Depth;

            var uavDesc = new UnorderedAccessViewDescription();

            uavDesc.Format                = Converter.Convert(format);
            uavDesc.Dimension             = UnorderedAccessViewDimension.Texture3D;
            uavDesc.Texture3D.FirstWSlice = 0;
            uavDesc.Texture3D.MipSlice    = 0;
            uavDesc.Texture3D.WSize       = depth;

            tex3D = new D3D.Texture3D(device.Device, texDesc);
            SRV   = new D3D.ShaderResourceView(device.Device, tex3D);
            uav   = new UnorderedAccessView(device.Device, tex3D, uavDesc);
        }
Esempio n. 3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="device"></param>
        /// <param name="size"></param>
        /// <param name="count"></param>
        /// <param name="format"></param>
        /// <param name="mips"></param>
        public TextureCubeArray(GraphicsDevice device, int size, int count, ColorFormat format, bool mips) : base(device)
        {
            if (count > 2048 / 6)
            {
                throw new GraphicsException("Too much elements in texture array");
            }

            this.Width    = size;
            this.Depth    = 1;
            this.Height   = size;
            this.MipCount = mips ? ShaderResource.CalculateMipLevels(Width, Height) : 1;

            var texDesc = new Texture2DDescription();

            texDesc.ArraySize                 = 6 * count;
            texDesc.BindFlags                 = BindFlags.ShaderResource;
            texDesc.CpuAccessFlags            = CpuAccessFlags.None;
            texDesc.Format                    = MakeTypeless(Converter.Convert(format));
            texDesc.Height                    = Height;
            texDesc.MipLevels                 = 0;
            texDesc.OptionFlags               = ResourceOptionFlags.TextureCube;
            texDesc.SampleDescription.Count   = 1;
            texDesc.SampleDescription.Quality = 0;
            texDesc.Usage = ResourceUsage.Default;
            texDesc.Width = Width;


            texCubeArray = new D3D.Texture2D(device.Device, texDesc);
            SRV          = new ShaderResourceView(device.Device, texCubeArray);
        }
Esempio n. 4
0
        /// <summary>
        /// Creates texture
        /// </summary>
        /// <param name="device"></param>
        public Texture2D(GraphicsDevice device, int width, int height, ColorFormat format, bool mips, bool srgb = false) : base(device)
        {
            this.Width    = width;
            this.Height   = height;
            this.Depth    = 1;
            this.format   = format;
            this.mipCount = mips ? ShaderResource.CalculateMipLevels(Width, Height) : 1;

            var texDesc = new Texture2DDescription();

            texDesc.ArraySize                 = 1;
            texDesc.BindFlags                 = BindFlags.ShaderResource;
            texDesc.CpuAccessFlags            = CpuAccessFlags.None;
            texDesc.Format                    = srgb ? MakeSRgb(Converter.Convert(format)) : Converter.Convert(format);
            texDesc.Height                    = Height;
            texDesc.MipLevels                 = mipCount;
            texDesc.OptionFlags               = ResourceOptionFlags.None;
            texDesc.SampleDescription.Count   = 1;
            texDesc.SampleDescription.Quality = 0;
            texDesc.Usage = ResourceUsage.Default;
            texDesc.Width = Width;

            lock (device.DeviceContext) {
                tex2D = new D3D.Texture2D(device.Device, texDesc);
                SRV   = new ShaderResourceView(device.Device, tex2D);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Creates texture
        /// </summary>
        /// <param name="device"></param>
        public Texture3D(GraphicsDevice device, int width, int height, int depth, ColorFormat format, bool mips, bool srgb = false) : base(device)
        {
            this.Width    = width;
            this.Height   = height;
            this.Depth    = depth;
            this.format   = format;
            this.mipCount = mips ? ShaderResource.CalculateMipLevels(Width, Height, Depth) : 1;

            var texDesc = new Texture3DDescription();

            texDesc.BindFlags      = BindFlags.ShaderResource;
            texDesc.CpuAccessFlags = CpuAccessFlags.None;
            texDesc.Format         = Converter.Convert(format);
            texDesc.Height         = Height;
            texDesc.MipLevels      = mipCount;
            texDesc.OptionFlags    = ResourceOptionFlags.None;
            texDesc.Usage          = ResourceUsage.Default;
            texDesc.Width          = Width;
            texDesc.Depth          = Depth;

            tex3D = new D3D.Texture3D(device.Device, texDesc);
            SRV   = new D3D.ShaderResourceView(device.Device, tex3D);
        }
Esempio n. 6
0
		/// <summary>
		/// 
		/// </summary>
		/// <param name="dst">target to copy to</param>
		/// <param name="src">target to copy from</param>
		public void Copy( RenderTargetSurface dst, ShaderResource src )
		{
			SetDefaultRenderStates();

			using( new PixEvent("Copy") ) {

				if(dst == null) {
					rs.RestoreBackbuffer();
				} else {
					SetViewport(dst);
					rs.SetTargets( null, dst );
				}

				rs.PipelineState			=	factory[ (int)ShaderFlags.COPY ];
				rs.PixelShaderResources[0]	= src;

				rs.Draw( 3, 0 );
			}
			rs.ResetStates();
		}
Esempio n. 7
0
		public void LinearizeDepth( RenderTargetSurface dst, ShaderResource src )
		{
			throw new NotImplementedException();
		#if false
			Debug.Assert( Game.IsServiceExist<Camera>() );

			var camera = Game.GetService<Camera>();

			bufLinearizeDepth.Data.linearizeDepthA = 1.0f / camera.FrustumZFar - 1.0f / camera.FrustumZNear;
			bufLinearizeDepth.Data.linearizeDepthB = 1.0f / camera.FrustumZNear;
			bufLinearizeDepth.UpdateCBuffer();


			var isDepthMSAA = ( src.SampleCount > 1 );
			var depthShader = (int)( isDepthMSAA ? ShaderFlags.RESOLVE_AND_LINEARIZE_DEPTH_MSAA : ShaderFlags.LINEARIZE_DEPTH );

			string signature;

			SetDefaultRenderStates();

			using( new PixEvent() ) {
				bufLinearizeDepth.SetCBufferPS( 0 );

				shaders.SetPixelShader( depthShader );
				shaders.SetVertexShader( depthShader );

				dst.SetViewport();
				rs.SetRenderTargets( dst );
				src.SetPS( 0 );

				rs.Draw( Primitive.TriangleList, 3, 0 );
			}
			rs.ResetStates();
		#endif
		}
Esempio n. 8
0
		/*-----------------------------------------------------------------------------------------------
		 * 
		 *	Base filters
		 * 
		-----------------------------------------------------------------------------------------------*/

		/// <summary>
		/// Performs good-old StretchRect to destination buffer with blending.
		/// </summary>
		/// <param name="dst"></param>
		/// <param name="src"></param>
		/// <param name="filter"></param>
		/// <param name="rect"></param>
		public void StretchRect( RenderTargetSurface dst, ShaderResource src, SamplerState filter = null, bool flipToCubeFace = false )
		{
			SetDefaultRenderStates();

			using( new PixEvent("StretchRect") ) {

				SetViewport(dst);
				rs.SetTargets( null, dst );

				if (flipToCubeFace) {
					rs.PipelineState		=	factory[ (int)(ShaderFlags.STRETCH_RECT|ShaderFlags.TO_CUBE_FACE) ];
				} else {
					rs.PipelineState		=	factory[ (int)ShaderFlags.STRETCH_RECT ];
				}
				rs.VertexShaderResources[0] =	src;
				rs.PixelShaderResources[0]	=	src;
				rs.PixelShaderSamplers[0]	=	filter ?? SamplerState.LinearPointClamp;
				rs.VertexShaderConstants[0]	=	sourceRectCB;

				rs.Draw( 3, 0 );
			}
			rs.ResetStates();
		}
Esempio n. 9
0
        /// <summary>
        /// Creates render target
        /// </summary>
        /// <param name="?"></param>
        /// <param name="format"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="samples"></param>
        /// <param name="mips"></param>
        /// <param name="debugName"></param>
        void Create(ColorFormat format, int size, int samples, bool mips, string debugName)
        {
            bool msaa = samples > 1;

            CheckSamplesCount(samples);

            if (mips && samples > 1)
            {
                throw new ArgumentException("Render target should be multisampler either mipmapped");
            }

            SampleCount = samples;

            Format      = format;
            SampleCount = samples;
            Width       = size;
            Height      = size;
            Depth       = 1;
            MipCount    = mips ? ShaderResource.CalculateMipLevels(Width, Height) : 1;

            var texDesc = new Texture2DDescription();

            texDesc.Width             = Width;
            texDesc.Height            = Height;
            texDesc.ArraySize         = 6;
            texDesc.BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource;
            texDesc.CpuAccessFlags    = CpuAccessFlags.None;
            texDesc.Format            = Converter.Convert(format);
            texDesc.MipLevels         = mips ? MipCount : 1;
            texDesc.OptionFlags       = ResourceOptionFlags.TextureCube | (mips ? ResourceOptionFlags.GenerateMipMaps : ResourceOptionFlags.None);
            texDesc.SampleDescription = new DXGI.SampleDescription(samples, 0);
            texDesc.Usage             = ResourceUsage.Default;


            texCube = new D3D.Texture2D(device.Device, texDesc);
            SRV     = new ShaderResourceView(device.Device, texCube);


            texDesc.BindFlags      = BindFlags.None;
            texDesc.CpuAccessFlags = CpuAccessFlags.Write | CpuAccessFlags.Read;
            texDesc.Usage          = ResourceUsage.Staging;
            texDesc.OptionFlags    = ResourceOptionFlags.TextureCube;

            staging = new D3D.Texture2D(device.Device, texDesc);


            //
            //	Top mipmap level :
            //
            cubeMipShaderResources = new ShaderResource[MipCount];

            for (int mip = 0; mip < MipCount; mip++)
            {
                var srvDesc = new ShaderResourceViewDescription();
                srvDesc.TextureCube.MipLevels       = 1;
                srvDesc.TextureCube.MostDetailedMip = mip;
                srvDesc.Format    = Converter.Convert(format);
                srvDesc.Dimension = ShaderResourceViewDimension.TextureCube;

                cubeMipShaderResources[mip] = new ShaderResource(device, new ShaderResourceView(device.Device, texCube, srvDesc), size >> mip, size >> mip, 1);
            }



            //
            //	Create surfaces :
            //
            surfaces = new RenderTargetSurface[MipCount, 6];

            for (int mip = 0; mip < MipCount; mip++)
            {
                int width  = GetMipSize(Width, mip);
                int height = GetMipSize(Height, mip);

                for (int face = 0; face < 6; face++)
                {
                    var rtvDesc = new RenderTargetViewDescription();
                    rtvDesc.Texture2DArray.MipSlice        = mip;
                    rtvDesc.Texture2DArray.FirstArraySlice = face;
                    rtvDesc.Texture2DArray.ArraySize       = 1;
                    rtvDesc.Dimension = msaa ? RenderTargetViewDimension.Texture2DMultisampledArray : RenderTargetViewDimension.Texture2DArray;
                    rtvDesc.Format    = Converter.Convert(format);

                    var rtv = new RenderTargetView(device.Device, texCube, rtvDesc);

                    int subResId = Resource.CalculateSubResourceIndex(mip, face, MipCount);

                    surfaces[mip, face] = new RenderTargetSurface(rtv, null, texCube, subResId, format, Width, Height, samples);

                    GraphicsDevice.Clear(surfaces[mip, face], Color4.Zero);
                }
            }
        }
Esempio n. 10
0
		/// <summary>
		/// Performs luminance measurement, tonemapping, applies bloom.
		/// </summary>
		/// <param name="target">LDR target.</param>
		/// <param name="hdrImage">HDR source image.</param>
		public void Render ( GameTime gameTime, RenderTargetSurface target, ShaderResource hdrImage, RenderWorld viewLayer )
		{
			var device	=	Game.GraphicsDevice;
			var filter	=	Game.RenderSystem.Filter;

			var settings	=	viewLayer.HdrSettings;

			//
			//	Rough downsampling of source HDR-image :
			//
			filter.StretchRect( averageLum.Surface, hdrImage, SamplerState.PointClamp );
			averageLum.BuildMipmaps();

			//
			//	Make bloom :
			//
			filter.StretchRect( viewLayer.Bloom0.Surface, hdrImage, SamplerState.LinearClamp );
			viewLayer.Bloom0.BuildMipmaps();

			filter.GaussBlur( viewLayer.Bloom0, viewLayer.Bloom1, settings.GaussBlurSigma, 0 );
			filter.GaussBlur( viewLayer.Bloom0, viewLayer.Bloom1, settings.GaussBlurSigma, 1 );
			filter.GaussBlur( viewLayer.Bloom0, viewLayer.Bloom1, settings.GaussBlurSigma, 2 );
			filter.GaussBlur( viewLayer.Bloom0, viewLayer.Bloom1, settings.GaussBlurSigma, 3 );

			//
			//	Setup parameters :
			//
			var paramsData	=	new Params();
			paramsData.AdaptationRate		=	1 - (float)Math.Pow( 0.5f, gameTime.ElapsedSec / settings.AdaptationHalfLife );
			paramsData.LuminanceLowBound	=	settings.LuminanceLowBound;
			paramsData.LuminanceHighBound	=	settings.LuminanceHighBound;
			paramsData.KeyValue				=	settings.KeyValue;
			paramsData.BloomAmount			=	settings.BloomAmount;
			paramsData.DirtMaskLerpFactor	=	settings.DirtMaskLerpFactor;
			paramsData.DirtAmount			=	settings.DirtAmount;

			paramsCB.SetData( paramsData );
			device.PixelShaderConstants[0]	=	paramsCB;

			//
			//	Measure and adapt :
			//
			device.SetTargets( null, viewLayer.MeasuredNew );

			device.PixelShaderResources[0]	=	averageLum;
			device.PixelShaderResources[1]	=	viewLayer.MeasuredOld;

			device.PipelineState		=	factory[ (int)(Flags.MEASURE_ADAPT) ];
				
			device.Draw( 3, 0 );


			//
			//	Tonemap and compose :
			//
			device.SetTargets( null, target );

			device.PixelShaderResources[0]	=	hdrImage;// averageLum;
			device.PixelShaderResources[1]	=	viewLayer.MeasuredNew;// averageLum;
			device.PixelShaderResources[2]	=	viewLayer.Bloom0;// averageLum;
			device.PixelShaderResources[3]	=	settings.DirtMask1==null ? whiteTex.Srv : settings.DirtMask1.Srv;
			device.PixelShaderResources[4]	=	settings.DirtMask2==null ? whiteTex.Srv : settings.DirtMask2.Srv;
			device.PixelShaderSamplers[0]	=	SamplerState.LinearClamp;

			Flags op = Flags.LINEAR;
			if (settings.TonemappingOperator==TonemappingOperator.Filmic)   { op = Flags.FILMIC;   }
			if (settings.TonemappingOperator==TonemappingOperator.Linear)   { op = Flags.LINEAR;	 }
			if (settings.TonemappingOperator==TonemappingOperator.Reinhard) { op = Flags.REINHARD; }

			device.PipelineState		=	factory[ (int)(Flags.TONEMAPPING|op) ];
				
			device.Draw( 3, 0 );
			
			device.ResetStates();


			//	swap luminanice buffers :
			Misc.Swap( ref viewLayer.MeasuredNew, ref viewLayer.MeasuredOld );
		}
Esempio n. 11
0
		/// <summary>
		/// 
		/// </summary>
		/// <param name="srcDst"></param>
		/// <param name="temporary"></param>
		/// <param name="?"></param>
		/// <param name="depthData"></param>
		/// <param name="normalData"></param>
		/// <param name="sigma"></param>
		/// <param name="mipLevel"></param>
		public void GaussBlurBilateral ( RenderTarget2D srcDst, RenderTarget2D temporary, ShaderResource depthData, ShaderResource normalData, float sigma, float sharpness, int mipLevel )
		{
			GaussBlurInternal( srcDst, temporary, sigma, sharpness, mipLevel, depthData, normalData );
		}
Esempio n. 12
0
        /// <summary>
        /// Creates render target
        /// </summary>
        /// <param name="?"></param>
        /// <param name="format"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="samples"></param>
        /// <param name="mips"></param>
        /// <param name="debugName"></param>
        void Create(ColorFormat format, int width, int height, int samples, int mips, bool enableRWBuffer)
        {
            //Log.Debug("RenderTarget2D: f:{0} w:{1} h:{2} s:{3}{4}{5}", format, width, height, samples, mips?" mips":"", enableRWBuffer?" uav":"" );

            bool msaa = samples > 1;

            CheckSamplesCount(samples);

            if (mips != 1 && samples > 1)
            {
                throw new ArgumentException("Render target should be multisampler either mipmapped");
            }

            SampleCount = samples;

            Format      = format;
            SampleCount = samples;
            Width       = width;
            Height      = height;
            Depth       = 1;
            MipCount    = mips == 0 ? ShaderResource.CalculateMipLevels(width, height) : mips;

            var texDesc = new Texture2DDescription();

            texDesc.Width             = width;
            texDesc.Height            = height;
            texDesc.ArraySize         = 1;
            texDesc.BindFlags         = BindFlags.RenderTarget | BindFlags.ShaderResource;
            texDesc.CpuAccessFlags    = CpuAccessFlags.None;
            texDesc.Format            = Converter.Convert(format);
            texDesc.MipLevels         = MipCount;
            texDesc.OptionFlags       = (mips != 1) ? ResourceOptionFlags.GenerateMipMaps : ResourceOptionFlags.None;
            texDesc.SampleDescription = new DXGI.SampleDescription(samples, 0);
            texDesc.Usage             = ResourceUsage.Default;

            if (enableRWBuffer)
            {
                texDesc.BindFlags |= BindFlags.UnorderedAccess;
            }


            tex2D = new D3D.Texture2D(device.Device, texDesc);
            SRV   = new ShaderResourceView(device.Device, tex2D);



            //
            //	Create surfaces :
            //
            surfaces = new RenderTargetSurface[MipCount];

            for (int i = 0; i < MipCount; i++)
            {
                width  = GetMipSize(Width, i);
                height = GetMipSize(Height, i);

                var rtvDesc = new RenderTargetViewDescription();
                rtvDesc.Texture2D.MipSlice = i;
                rtvDesc.Dimension          = msaa ? RenderTargetViewDimension.Texture2DMultisampled : RenderTargetViewDimension.Texture2D;
                rtvDesc.Format             = Converter.Convert(format);

                var rtv = new RenderTargetView(device.Device, tex2D, rtvDesc);

                UnorderedAccessView uav = null;

                if (enableRWBuffer)
                {
                    var uavDesc = new UnorderedAccessViewDescription();
                    uavDesc.Buffer.ElementCount = width * height;
                    uavDesc.Buffer.FirstElement = 0;
                    uavDesc.Buffer.Flags        = UnorderedAccessViewBufferFlags.None;
                    uavDesc.Dimension           = UnorderedAccessViewDimension.Texture2D;
                    uavDesc.Format             = Converter.Convert(format);
                    uavDesc.Texture2D.MipSlice = i;

                    uav = new UnorderedAccessView(device.Device, tex2D, uavDesc);
                }

                surfaces[i] = new RenderTargetSurface(rtv, uav, tex2D, i, format, width, height, samples);
            }
        }
Esempio n. 13
0
		/// <summary>
		/// 
		/// </summary>
		void RenderGeneric ( string passName, GameTime gameTime, Camera camera, Viewport viewport, Matrix view, Matrix projection, RenderTargetSurface colorTarget, DepthStencilSurface depthTarget, ShaderResource depthValues, Flags flags )
		{
			var device	=	Game.GraphicsDevice;

			if (rs.SkipParticles) {
				return;
			}


			using ( new PixEvent(passName) ) {

				device.ResetStates();

				//
				//	Setup images :
				//
				if (Images!=null && !Images.IsDisposed) {
					imagesCB.SetData( Images.GetNormalizedRectangles( MaxImages ) );
				}

				SetupGPUParameters( 0, renderWorld, view, projection, flags );
				device.ComputeShaderConstants[0] = paramsCB ;

				//
				//	Render
				//
				using (new PixEvent("Drawing")) {

					device.ResetStates();
	
					//	target and viewport :
					device.SetTargets( depthTarget, colorTarget );
					device.SetViewport( viewport );

					//	params CB :			
					device.ComputeShaderConstants[0]	= paramsCB ;
					device.VertexShaderConstants[0]		= paramsCB ;
					device.GeometryShaderConstants[0]	= paramsCB ;
					device.PixelShaderConstants[0]		= paramsCB ;

					//	atlas CB :
					device.VertexShaderConstants[1]		= imagesCB ;
					device.GeometryShaderConstants[1]	= imagesCB ;
					device.PixelShaderConstants[1]		= imagesCB ;

					//	sampler & textures :
					device.PixelShaderSamplers[0]		=	SamplerState.LinearClamp4Mips ;

					device.PixelShaderResources[0]		=	Images==null? rs.WhiteTexture.Srv : Images.Texture.Srv;
					device.PixelShaderResources[5]		=	depthValues;
					device.GeometryShaderResources[1]	=	simulationBuffer ;
					device.GeometryShaderResources[2]	=	simulationBuffer ;
					device.GeometryShaderResources[3]	=	sortParticlesBuffer;
					device.GeometryShaderResources[4]	=	particleLighting;

					//	setup PS :
					device.PipelineState	=	factory[ (int)flags ];

					//	GPU time : 0.81 ms	-> 0.91 ms
					device.Draw( MaxSimulatedParticles, 0 );
				}
			}
		}
Esempio n. 14
0
		/// <summary>
		/// Applies DOF effect
		/// </summary>
		public void Render ( GameTime gameTime, RenderTarget2D temp, RenderTarget2D hdrImage, ShaderResource depthBuffer, RenderWorld renderWorld )
		{
			if (!renderWorld.DofSettings.Enabled) {
				return;
			}

			var device	=	Game.GraphicsDevice;
			var filter	=	Game.RenderSystem.Filter;

			device.ResetStates();


			//
			//	Setup parameters :
			//
			var paramsData	=	new Params();
			paramsData.LinearDepthBias	=	renderWorld.Camera.LinearizeDepthBias;
			paramsData.LinearDepthScale	=	renderWorld.Camera.LinearizeDepthScale;	
			paramsData.CocBias			=	renderWorld.DofSettings.CocBias;
			paramsData.CocScale			=	renderWorld.DofSettings.CocScale;

			paramsCB.SetData( paramsData );
			device.PixelShaderConstants[0]	=	paramsCB;

			//
			//	Compute COC and write it in alpha channel :
			//
			device.SetTargets( (DepthStencilSurface)null, hdrImage.Surface );

			device.PixelShaderResources[0]	=	depthBuffer;
			device.PipelineState			=	factory[ (int)(Flags.COC_TO_ALPHA) ];
				
			device.Draw( 3, 0 );


			//
			//	Perform DOF :
			//
			device.SetTargets( null, temp );

			device.PixelShaderResources[0]	=	hdrImage;
			device.PixelShaderSamplers[0]	=	SamplerState.LinearClamp;
			device.VertexShaderResources[0]	=	hdrImage;
			device.VertexShaderSamplers[0]	=	SamplerState.LinearClamp;

			device.PipelineState			=	factory[ (int)(Flags.DEPTH_OF_FIELD) ];
				
			device.Draw( 3, 0 );
			
			device.ResetStates();


			//
			//	Copy DOFed image back to source :
			//
			filter.Copy( hdrImage.Surface, temp );
		}
Esempio n. 15
0
		/// <summary>
		/// Creates render target
		/// </summary>
		/// <param name="?"></param>
		/// <param name="format"></param>
		/// <param name="width"></param>
		/// <param name="height"></param>
		/// <param name="samples"></param>
		/// <param name="mips"></param>
		/// <param name="debugName"></param>
		void Create ( ColorFormat format, int size, int samples, bool mips, string debugName )
		{
			bool msaa	=	samples > 1;

			CheckSamplesCount( samples );

			if (mips && samples>1) {
				throw new ArgumentException("Render target should be multisampler either mipmapped");
			}

			SampleCount		=	samples;

			Format		=	format;
			SampleCount	=	samples;
			Width		=	size;
			Height		=	size;
			Depth		=	1;
			MipCount	=	mips ? ShaderResource.CalculateMipLevels( Width, Height ) : 1;

			var	texDesc	=	new Texture2DDescription();
				texDesc.Width				=	Width;
				texDesc.Height				=	Height;
				texDesc.ArraySize			=	6;
				texDesc.BindFlags			=	BindFlags.RenderTarget | BindFlags.ShaderResource;
				texDesc.CpuAccessFlags		=	CpuAccessFlags.None;
				texDesc.Format				=	Converter.Convert( format );
				texDesc.MipLevels			=	mips ? MipCount : 1;
				texDesc.OptionFlags			=	ResourceOptionFlags.TextureCube | (mips ? ResourceOptionFlags.GenerateMipMaps : ResourceOptionFlags.None);
				texDesc.SampleDescription	=	new DXGI.SampleDescription(samples, 0);
				texDesc.Usage				=	ResourceUsage.Default;


			texCube	=	new D3D.Texture2D( device.Device, texDesc );
			SRV		=	new ShaderResourceView( device.Device, texCube );


			//
			//	Top mipmap level :
			//
			cubeMipShaderResources = new ShaderResource[MipCount];

			for (int mip=0; mip<MipCount; mip++) {

				var srvDesc = new ShaderResourceViewDescription();
					srvDesc.TextureCube.MipLevels		=	1;
					srvDesc.TextureCube.MostDetailedMip	=	mip;
					srvDesc.Format		=	Converter.Convert( format );
					srvDesc.Dimension	=	ShaderResourceViewDimension.TextureCube;

				cubeMipShaderResources[mip]	=	new ShaderResource( device, new ShaderResourceView(device.Device, texCube, srvDesc), size>>mip, size>>mip, 1 );
			}



			//
			//	Create surfaces :
			//
			surfaces	=	new RenderTargetSurface[ MipCount, 6 ];

			for ( int mip=0; mip<MipCount; mip++ ) { 

				int width	=	GetMipSize( Width,  mip );
				int height	=	GetMipSize( Height, mip );

				for ( int face=0; face<6; face++) {

					var rtvDesc = new RenderTargetViewDescription();
						rtvDesc.Texture2DArray.MipSlice			=	mip;
						rtvDesc.Texture2DArray.FirstArraySlice	=	face;
						rtvDesc.Texture2DArray.ArraySize		=	1;
						rtvDesc.Dimension						=	msaa ? RenderTargetViewDimension.Texture2DMultisampledArray : RenderTargetViewDimension.Texture2DArray;
						rtvDesc.Format							=	Converter.Convert( format );

					var rtv	=	new RenderTargetView( device.Device, texCube, rtvDesc );

					int subResId	=	Resource.CalculateSubResourceIndex( mip, face, MipCount );

					surfaces[mip,face]	=	new RenderTargetSurface( rtv, null, texCube, subResId, format, Width, Height, samples );

					GraphicsDevice.Clear( surfaces[mip,face], Color4.Zero );
				}
			}
		}
Esempio n. 16
0
		/// <summary>
		/// 
		/// </summary>
		/// <param name="dst">target to copy to</param>
		/// <param name="src">target to copy from</param>
		public void OverlayAdditive( RenderTargetSurface dst, ShaderResource src )
		{
			SetDefaultRenderStates();

			using( new PixEvent("OverlayAdditive") ) {

				if(dst == null) {
					rs.RestoreBackbuffer();
				} else {
					rs.SetTargets( null, dst );
				}

				rs.PipelineState			=	factory[ (int)ShaderFlags.OVERLAY_ADDITIVE ];
				rs.PixelShaderResources[0]	=	src;

				rs.Draw( 3, 0 );
			}
			rs.ResetStates();
		}
Esempio n. 17
0
		/// <summary>
		/// Performs FXAA antialiasing.
		/// </summary>
		/// <param name="dst">Target buffer to render FXAA to</param>
		/// <param name="src">Source image with luminance in alpha</param>
		public void Fxaa( RenderTargetSurface dst, ShaderResource src )
		{
			SetDefaultRenderStates();

			using( new PixEvent("Fxaa") ) {

				if(dst == null) {
					rs.RestoreBackbuffer();
				} else {
					SetViewport( dst );
					rs.SetTargets( null, dst );
				}

				rs.PipelineState			=	factory[ (int)ShaderFlags.FXAA ];
				rs.VertexShaderResources[0] =	src;
				rs.PixelShaderResources[0]	=	src;
				rs.PixelShaderSamplers[0]	=	SamplerState.LinearPointClamp;

				rs.Draw( 3, 0 );
			}
			rs.ResetStates();
		}
Esempio n. 18
0
        public void RenderBlank(Matrix view, Matrix projection, ShaderResource depthBuffer, ShaderResource wsNormals)
		{
			var device = Game.GraphicsDevice;
			var filter = Game.RenderSystem.Filter;

			filter.StretchRect(downsampledDepth.Surface, depthBuffer);
			filter.StretchRect(downsampledNormals.Surface, wsNormals);


			//
			//	Setup parameters :
			//
			var paramsData = new Params();
			paramsData.ProjMatrix = projection;
			paramsData.View = view;
			paramsData.ViewProj = view * projection;
			paramsData.InvViewProj = Matrix.Invert(view * projection);
			paramsData.InvProj = Matrix.Invert( projection );
			//paramsData.TraceStep = Config.TraceStep;
			//paramsData.DecayRate = Config.DecayRate;
			paramsData.MaxSampleRadius	= MaxSamplingRadius;
			paramsData.MaxDepthJump		= MaxDepthJump;

			paramsCB.SetData(paramsData);
            sampleDirectionsCB.SetData(sampleDirectionData);

			device.PixelShaderConstants[0] = paramsCB;
            device.PixelShaderConstants[1] = sampleDirectionsCB;
			//
			//	Measure and adapt :
			//
			device.SetTargets(null, occlusionMap0);

			device.PixelShaderResources[0] = downsampledDepth;
			device.PixelShaderResources[1] = downsampledNormals;
			device.PixelShaderResources[2] = randomDir;
			device.PixelShaderSamplers[0] = SamplerState.LinearClamp;
			device.PipelineState = factory[(int)Flags.BLANK];


			device.Draw(3, 0);

			device.ResetStates();
		}
Esempio n. 19
0
		/// <summary>
		/// 
		/// </summary>
		/// <param name="srcDst"></param>
		/// <param name="temporary"></param>
		/// <param name="sigma"></param>
		/// <param name="kernelSize"></param>
		void GaussBlurInternal ( RenderTarget2D srcDst, RenderTarget2D temporary, float sigma, float sharpness, int mipLevel, ShaderResource depthData, ShaderResource normalData )
		{
			var taps = GetGaussWeightsBuffer( sigma, mipLevel );

			SetDefaultRenderStates();

			gaussWeightsCB.SetData( taps );


			int combination	=	(int)ShaderFlags.GAUSS_BLUR;

			if (depthData!=null && normalData!=null) {
				combination |=	(int)ShaderFlags.BILATERAL;
			}



			using( new PixEvent("GaussBlur") ) {

				SetViewport(temporary.GetSurface(mipLevel));
				rs.SetTargets( null, temporary.GetSurface(mipLevel) );

				rs.PipelineState			=	factory[ combination|(int)ShaderFlags.PASS1 ];
				rs.VertexShaderResources[0]	=	srcDst;
				rs.PixelShaderResources[0]	=	srcDst;
				rs.PixelShaderResources[1]	=	depthData;
				rs.PixelShaderResources[2]	=	normalData;

				rs.PixelShaderConstants[0]	=	gaussWeightsCB;
				
				rs.PixelShaderSamplers[0]	=	SamplerState.LinearPointClamp;
				rs.PixelShaderSamplers[1]	=	SamplerState.PointClamp;

				rs.Draw( 3, 0 );



				rs.VertexShaderResources[0] =	null;
				rs.PixelShaderResources[0]	=	null;

				SetViewport(srcDst.GetSurface(mipLevel));
				rs.SetTargets( null, srcDst.GetSurface(mipLevel) );

				rs.PipelineState			=	factory[ combination|(int)ShaderFlags.PASS2 ];
				rs.VertexShaderResources[0] =	temporary;
				rs.PixelShaderResources[0]	=	temporary;
				rs.PixelShaderResources[1]	=	depthData;
				rs.PixelShaderResources[2]	=	normalData;

				rs.PixelShaderConstants[0]	=	gaussWeightsCB;

				rs.PixelShaderSamplers[0]	=	SamplerState.LinearPointClamp;
				rs.PixelShaderSamplers[1]	=	SamplerState.PointClamp;

				rs.Draw( 3, 0 );
			}
			rs.ResetStates();
		}
Esempio n. 20
0
		/// <summary>
		/// Performs luminance measurement, tonemapping, applies bloom.
		/// </summary>
		/// <param name="target">LDR target.</param>
		/// <param name="hdrImage">HDR source image.</param>
		public void Render ( StereoEye stereoEye, Camera camera, ShaderResource depthBuffer, ShaderResource wsNormals )
		{
			var view		=	camera.GetViewMatrix( stereoEye );
			var projection	=	camera.GetProjectionMatrix( stereoEye );

			
			var device	=	Game.GraphicsDevice;
			var filter	=	Game.RenderSystem.Filter;

			if (!Enabled) {
				device.Clear( occlusionMap0.Surface, Color4.White );
				return;
			}

			using (new PixEvent("SSAO Render")) {
				filter.StretchRect( downsampledDepth.Surface, depthBuffer );
				filter.StretchRect( downsampledNormals.Surface, wsNormals );

				using (new PixEvent("SSAO Pass")) {

					//
					//	Setup parameters :
					//
					var paramsData	=	new Params();
					paramsData.ProjMatrix	=	projection;
					paramsData.View			=	view;
					paramsData.ViewProj		=	view * projection;
					paramsData.InvViewProj	=	Matrix.Invert( view * projection );
					paramsData.InvProj = Matrix.Invert(projection);
					//paramsData.TraceStep	=	Config.TraceStep;
					//paramsData.DecayRate	=	Config.DecayRate;
					paramsData.MaxSampleRadius	= MaxSamplingRadius;
					paramsData.MaxDepthJump		= MaxDepthJump;

					paramsCB.SetData( paramsData );
					sampleDirectionsCB.SetData(sampleDirectionData);

					device.PixelShaderConstants[0]	=	paramsCB;
					device.PixelShaderConstants[1]  =   sampleDirectionsCB;

					//
					//	Measure and adapt :
					//
					device.SetTargets( null, occlusionMap0 );

					device.PixelShaderResources[0]	=	downsampledDepth;
					device.PixelShaderResources[1]	=	downsampledNormals;
					device.PixelShaderResources[2]	=	randomDir;
					device.PixelShaderSamplers[0]	=	SamplerState.LinearClamp;

					Flags sampleNumFlag = getSampleNumFlag();

					device.PipelineState = factory[getFlags()];
			
					device.Draw( 3, 0 );
			
					device.ResetStates();
				}

				using (new PixEvent("Bilateral Filter")) {
					if (BlurSigma!=0) {
						filter.GaussBlurBilateral( occlusionMap0, occlusionMap1, downsampledDepth, downsampledNormals, BlurSigma, Sharpness, 0 );
					}
				}
			}
		}