Represents
Inheritance: System.DisposableBase
示例#1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public void Resize(int newWidth, int newHeight)
        {
            SafeDispose(ref viewHdrFrame);

            SafeDispose(ref Bloom0);
            SafeDispose(ref Bloom1);

            //	clamp values :
            newWidth  = Math.Max(128, newWidth);
            newHeight = Math.Max(128, newHeight);

            int targetWidth  = newWidth;
            int targetHeight = newHeight;

            int bloomWidth  = (targetWidth / 2) & 0xFFF0;
            int bloomHeight = (targetHeight / 2) & 0xFFF0;

            viewHdrFrame = new HdrFrame(Game, targetWidth, targetHeight);

            Bloom0 = new RenderTarget2D(Game.GraphicsDevice, ColorFormat.Rgba16F, bloomWidth, bloomHeight, true, false);
            Bloom1 = new RenderTarget2D(Game.GraphicsDevice, ColorFormat.Rgba16F, bloomWidth, bloomHeight, true, false);

            //HdrTexture			=	new TargetTexture( HdrBuffer );
            //DiffuseTexture		=	new TargetTexture( DiffuseBuffer );
            //SpecularTexture		=	new TargetTexture( SpecularBuffer );
            //NormalMapTexture	=	new TargetTexture( NormalMapBuffer );
        }
示例#2
0
        /// <summary>
        ///
        /// </summary>
        void ClearBuffers(HdrFrame frame)
        {
            Game.GraphicsDevice.Clear(frame.GBuffer0.Surface, Color4.Black);
            Game.GraphicsDevice.Clear(frame.GBuffer1.Surface, Color4.Black);

            Game.GraphicsDevice.Clear(frame.FeedbackBufferRB.Surface, Color4.Zero);

            Game.GraphicsDevice.Clear(frame.FeedbackBuffer.Surface, Color4.Zero);

            Game.GraphicsDevice.Clear(frame.DepthBuffer.Surface, 1, 0);
            Game.GraphicsDevice.Clear(frame.HdrBuffer.Surface, Color4.Black);
        }
示例#3
0
文件: Sky.cs 项目: ttou73/IronStar
        /// <summary>
        /// Renders sky with specified technique
        /// </summary>
        /// <param name="rendCtxt"></param>
        /// <param name="techName"></param>
        internal void Render(Camera camera, StereoEye stereoEye, HdrFrame frame, SkySettings settings)
        {
            using (new PixEvent("Sky Rendering")) {
                var scale    = Matrix.Scaling(settings.SkySphereSize);
                var rotation = Matrix.Identity;

                var sunPos   = settings.SunPosition;
                var sunColor = settings.SunGlowColor;

                device.ResetStates();

                //rs.DepthStencilState = depthBuffer==null? DepthStencilState.None : DepthStencilState.Default ;

                device.SetTargets(frame.DepthBuffer.Surface, frame.HdrBuffer.Surface);

                var viewMatrix = camera.GetViewMatrix(stereoEye);
                var projMatrix = camera.GetProjectionMatrix(stereoEye);

                skyConstsData.MatrixWVP    = scale * rotation * MathUtil.Transformation(viewMatrix.Right, viewMatrix.Up, viewMatrix.Backward) * projMatrix;
                skyConstsData.SunPosition  = sunPos;
                skyConstsData.SunColor     = sunColor;
                skyConstsData.Turbidity    = settings.SkyTurbidity;
                skyConstsData.Temperature  = Temperature.Get(settings.SunTemperature);
                skyConstsData.SkyIntensity = settings.SkyIntensity;

                skyConstsCB.SetData(skyConstsData);

                device.VertexShaderConstants[0] = skyConstsCB;
                device.PixelShaderConstants[0]  = skyConstsCB;


                //
                //	Sky :
                //
                SkyFlags flags = SkyFlags.SKY;

                ApplyColorSpace(ref flags, settings);

                device.PipelineState = factory[(int)flags];

                device.SetupVertexInput(skyVB, null);
                device.Draw(skyVB.Capacity, 0);

                device.ResetStates();
            }
        }
示例#4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="gameTime"></param>
        internal void Render(GameTime gameTime, Camera camera, StereoEye stereoEye, HdrFrame viewFrame)
        {
            if (rs.SkipParticles)
            {
                return;
            }

            var view       = camera.GetViewMatrix(stereoEye);
            var projection = camera.GetProjectionMatrix(stereoEye);

            var colorTarget = viewFrame.HdrBuffer.Surface;
            var depthTarget = viewFrame.DepthBuffer.Surface;

            var viewport = new Viewport(0, 0, colorTarget.Width, colorTarget.Height);

            RenderGeneric("Particles", gameTime, camera, viewport, view, projection, colorTarget, null, viewFrame.DepthBuffer, Flags.DRAW);
        }
示例#5
0
        /// <summary>
        /// Creates ViewLayerHDR instance
        /// </summary>
        /// <param name="Game">Game engine</param>
        /// <param name="width">Target width.</param>
        /// <param name="height">Target height.</param>
        public RenderWorld(Game game, int width, int height)
        {
            Game    = game;
            this.rs = Game.RenderSystem;

            Camera = new Camera();

            var vp = Game.GraphicsDevice.DisplayBounds;

            if (width <= 0)
            {
                width = vp.Width;
            }
            if (height <= 0)
            {
                height = vp.Height;
            }

            HdrSettings = new HdrSettings();
            SkySettings = new SkySettings();
            DofSettings = new DofSettings();
            FogSettings = new FogSettings();

            Instances = new List <MeshInstance>();
            LightSet  = new LightSet(Game.RenderSystem);

            debug = new DebugRender(Game);

            particleSystem = new ParticleSystem(Game.RenderSystem, this);

            MeasuredOld = new RenderTarget2D(Game.GraphicsDevice, ColorFormat.Rgba32F, 1, 1);
            MeasuredNew = new RenderTarget2D(Game.GraphicsDevice, ColorFormat.Rgba32F, 1, 1);

            radianceFrame = new HdrFrame(Game, 512, 512);

            Radiance      = new RenderTargetCube(Game.GraphicsDevice, ColorFormat.Rgba16F, RenderSystem.EnvMapSize, true);
            RadianceCache = new TextureCubeArray(Game.GraphicsDevice, 128, RenderSystem.MaxEnvLights, ColorFormat.Rgba16F, true);

            Resize(width, height);
        }
示例#6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="camera"></param>
        /// <param name="depthBuffer"></param>
        /// <param name="hdrTarget"></param>
        /// <param name="diffuse"></param>
        /// <param name="specular"></param>
        /// <param name="normals"></param>
        internal void RenderGBuffer(GameTime gameTime, StereoEye stereoEye, Camera camera, HdrFrame frame, RenderWorld rw, bool staticOnly)
        {
            using (new PixEvent("RenderGBuffer")) {
                if (surfaceShader == null)
                {
                    return;
                }

                if (rs.SkipSceneRendering)
                {
                    return;
                }

                var device = Game.GraphicsDevice;

                var view         = camera.GetViewMatrix(stereoEye);
                var projection   = camera.GetProjectionMatrix(stereoEye);
                var viewPosition = camera.GetCameraPosition4(stereoEye);

                var cbData       = new CBMeshInstanceData();
                var cbDataSubset = new CBSubsetData();

                var hdr      = frame.HdrBuffer.Surface;
                var depth    = frame.DepthBuffer.Surface;
                var gbuffer0 = frame.GBuffer0.Surface;
                var gbuffer1 = frame.GBuffer1.Surface;
                var feedback = frame.FeedbackBuffer.Surface;

                device.ResetStates();

                device.SetTargets(depth, hdr, gbuffer0, gbuffer1, feedback);
                device.PixelShaderSamplers[0] = SamplerState.LinearPointClamp;
                device.PixelShaderSamplers[1] = SamplerState.PointClamp;
                device.PixelShaderSamplers[2] = SamplerState.AnisotropicClamp;

                var instances = rw.Instances;

                if (instances.Any())
                {
                    device.PixelShaderResources[1] = rs.VTSystem.PageTable;
                    device.PixelShaderResources[2] = rs.VTSystem.PhysicalPages0;
                    device.PixelShaderResources[3] = rs.VTSystem.PhysicalPages1;
                    device.PixelShaderResources[4] = rs.VTSystem.PhysicalPages2;
                }

                //#warning INSTANSING!
                foreach (var instance in instances)
                {
                    if (!instance.Visible)
                    {
                        continue;
                    }

                    if (staticOnly && !instance.Static)
                    {
                        continue;
                    }

                    cbData.View           = view;
                    cbData.Projection     = projection;
                    cbData.World          = instance.World;
                    cbData.ViewPos        = viewPosition;
                    cbData.Color          = instance.Color;
                    cbData.ViewBounds     = new Vector4(hdr.Width, hdr.Height, hdr.Width, hdr.Height);
                    cbData.VTPageScaleRCP = rs.VTSystem.PageScaleRCP;

                    constBuffer.SetData(cbData);

                    device.PixelShaderConstants[0]  = constBuffer;
                    device.VertexShaderConstants[0] = constBuffer;
                    device.PixelShaderConstants[1]  = constBufferSubset;

                    device.SetupVertexInput(instance.vb, instance.ib);

                    if (instance.IsSkinned)
                    {
                        constBufferBones.SetData(instance.BoneTransforms);
                        device.VertexShaderConstants[3] = constBufferBones;
                    }

                    try {
                        device.PipelineState = factory[(int)(SurfaceFlags.GBUFFER | SurfaceFlags.RIGID)];


                        foreach (var subset in instance.Subsets)
                        {
                            var vt   = rw.VirtualTexture;
                            var rect = vt.GetTexturePosition(subset.Name);

                            cbDataSubset.Rectangle = new Vector4(rect.X, rect.Y, rect.Width, rect.Height);

                            constBufferSubset.SetData(cbDataSubset);
                            device.PixelShaderConstants[1] = constBufferSubset;

                            device.DrawIndexed(subset.PrimitiveCount * 3, subset.StartPrimitive * 3, 0);
                        }

                        rs.Counters.SceneDIPs++;
                    } catch (UbershaderException e) {
                        Log.Warning(e.Message);
                        ExceptionDialog.Show(e);
                    }
                }


                //
                //	downsample feedback buffer and readback it to virtual texture :
                //
                rs.Filter.StretchRect(frame.FeedbackBufferRB.Surface, frame.FeedbackBuffer, SamplerState.PointClamp);

                var feedbackBuffer = new VTAddress[HdrFrame.FeedbackBufferWidth * HdrFrame.FeedbackBufferHeight];
                frame.FeedbackBufferRB.GetFeedback(feedbackBuffer);
                rs.VTSystem.Update(feedbackBuffer, gameTime);
            }
        }
示例#7
0
		/// <summary>
		/// 
		/// </summary>
		/// <param name="camera"></param>
		/// <param name="depthBuffer"></param>
		/// <param name="hdrTarget"></param>
		/// <param name="diffuse"></param>
		/// <param name="specular"></param>
		/// <param name="normals"></param>
		internal void RenderGBuffer ( StereoEye stereoEye, Camera camera, HdrFrame frame, RenderWorld viewLayer )
		{		
			using ( new PixEvent("RenderGBuffer") ) {
				if (surfaceShader==null) {	
					return;
				}

				if (rs.SkipSceneRendering) {
					return;
				}

				var device		=	Game.GraphicsDevice;

				var view			=	camera.GetViewMatrix( stereoEye );
				var projection		=	camera.GetProjectionMatrix( stereoEye );
				var viewPosition	=	camera.GetCameraPosition4( stereoEye );

				var cbData		=	new CBMeshInstanceData();

				var hdr			=	frame.HdrBuffer.Surface;
				var depth		=	frame.DepthBuffer.Surface;
				var diffuse		=	frame.DiffuseBuffer.Surface;
				var specular	=	frame.SpecularBuffer.Surface;
				var normals		=	frame.NormalMapBuffer.Surface;
				var scattering	=	frame.ScatteringBuffer.Surface;

				device.ResetStates();

				device.SetTargets( depth, hdr, diffuse, specular, normals, scattering );
				device.PixelShaderSamplers[0]	= SamplerState.AnisotropicWrap ;

				var instances	=	viewLayer.Instances;

				//#warning INSTANSING!
				foreach ( var instance in instances ) {

					if (!instance.Visible) {
						continue;
					}

					cbData.View			=	view;
					cbData.Projection	=	projection;
					cbData.World		=	instance.World;
					cbData.ViewPos		=	viewPosition;
					
					constBuffer.SetData( cbData );

					device.PixelShaderConstants[0]	= constBuffer ;
					device.VertexShaderConstants[0]	= constBuffer ;

					device.SetupVertexInput( instance.vb, instance.ib );

					if (instance.IsSkinned) {
						constBufferBones.SetData( instance.BoneTransforms );
						device.VertexShaderConstants[3]	= constBufferBones ;
					}

					try {

						foreach ( var sg in instance.ShadingGroups ) {

							device.PipelineState	=	instance.IsSkinned ? sg.Material.GBufferSkinned : sg.Material.GBufferRigid;

							device.PixelShaderConstants[1]	= sg.Material.ConstantBufferParameters;
							device.PixelShaderConstants[2]	= sg.Material.ConstantBufferUVModifiers;
							device.VertexShaderConstants[1]	= sg.Material.ConstantBufferParameters;
							device.VertexShaderConstants[2]	= sg.Material.ConstantBufferUVModifiers;

							sg.Material.SetTextures( device );

							device.DrawIndexed( sg.IndicesCount, sg.StartIndex, 0 );

							rs.Counters.SceneDIPs++;
						}
					} catch ( UbershaderException e ) {
						Log.Warning( e.Message );					
						ExceptionDialog.Show( e );
					}
				}
			}
		}
示例#8
0
		/// <summary>
		/// 
		/// </summary>
		/// <param name="gameTime"></param>
		internal void Render ( GameTime gameTime, Camera camera, StereoEye stereoEye, HdrFrame viewFrame )
		{
			var view		=	camera.GetViewMatrix( stereoEye );
			var projection	=	camera.GetProjectionMatrix( stereoEye );

			var colorTarget	=	viewFrame.HdrBuffer.Surface;
			var depthTarget	=	viewFrame.DepthBuffer.Surface;

			var viewport	=	new Viewport( 0, 0, colorTarget.Width, colorTarget.Height );

			RenderGeneric( "Particles", gameTime, camera, viewport, view, projection, colorTarget, null, viewFrame.DepthBuffer, Flags.DRAW );
		}
示例#9
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="view"></param>
        /// <param name="projection"></param>
        internal void RenderLighting(StereoEye stereoEye, Camera camera, HdrFrame hdrFrame, RenderWorld viewLayer, RenderTargetCube envLight)
        {
            using (new PixEvent("TiledLighting")) {
                var view       = camera.GetViewMatrix(stereoEye);
                var projection = camera.GetProjectionMatrix(stereoEye);

                var device = Game.GraphicsDevice;
                device.ResetStates();

                var width  = hdrFrame.HdrBuffer.Width;
                var height = hdrFrame.HdrBuffer.Height;


                ICSMController csmCtrl = viewLayer.LightSet.DirectLight.CSMController ?? csmController;

                int activeCascadeCount = Math.Min(cascadedShadowMap.CascadeCount, csmCtrl.GetActiveCascadeCount());


                //
                //	Setup compute shader parameters and states :
                //
                try {
                    var cbData  = new LightingParams();
                    var invView = Matrix.Invert(view);
                    var invVP   = Matrix.Invert(view * projection);
                    var viewPos = invView.TranslationVector;

                    cbData.DirectLightDirection = new Vector4(viewLayer.LightSet.DirectLight.Direction, 0);
                    cbData.DirectLightIntensity = viewLayer.LightSet.DirectLight.Intensity.ToVector4();
                    cbData.Projection           = projection;

                    cbData.CSMViewProjection0 = csmCtrl.GetShadowViewMatrix(0) * csmCtrl.GetShadowProjectionMatrix(0);
                    cbData.CSMViewProjection1 = csmCtrl.GetShadowViewMatrix(1) * csmCtrl.GetShadowProjectionMatrix(1);
                    cbData.CSMViewProjection2 = csmCtrl.GetShadowViewMatrix(2) * csmCtrl.GetShadowProjectionMatrix(2);
                    cbData.CSMViewProjection3 = csmCtrl.GetShadowViewMatrix(3) * csmCtrl.GetShadowProjectionMatrix(3);

                    cbData.View                  = view;
                    cbData.ViewPosition          = new Vector4(viewPos, 1);
                    cbData.InverseViewProjection = invVP;
                    cbData.CSMFilterRadius       = new Vector4(CSMFilterSize);

                    cbData.AmbientColor   = viewLayer.LightSet.AmbientLevel;
                    cbData.Viewport       = new Vector4(0, 0, width, height);
                    cbData.ShowCSLoadOmni = ShowOmniLightTileLoad ? 1 : 0;
                    cbData.ShowCSLoadEnv  = ShowEnvLightTileLoad  ? 1 : 0;
                    cbData.ShowCSLoadSpot = ShowSpotLightTileLoad ? 1 : 0;

                    cbData.CascadeCount = activeCascadeCount;
                    cbData.CascadeScale = 1.0f / (float)cascadedShadowMap.CascadeCount;

                    cbData.FogDensity = viewLayer.FogSettings.Density;


                    ComputeOmniLightsTiles(view, projection, viewLayer.LightSet);
                    ComputeSpotLightsTiles(view, projection, viewLayer.LightSet);
                    ComputeEnvLightsTiles(view, projection, viewLayer.LightSet);
                    ComputeDecalTiles(view, projection, viewLayer.LightSet);


                    //
                    //	set states :
                    //
                    device.SetTargets(null, hdrFrame.HdrBuffer.Surface);

                    lightingCB.SetData(cbData);

                    device.ComputeShaderSamplers[0] = SamplerState.PointClamp;
                    device.ComputeShaderSamplers[1] = SamplerState.LinearClamp;
                    device.ComputeShaderSamplers[2] = SamplerState.ShadowSampler;
                    device.ComputeShaderSamplers[3] = SamplerState.LinearPointWrap;

                    device.ComputeShaderResources[0]  = hdrFrame.GBuffer0;
                    device.ComputeShaderResources[1]  = hdrFrame.GBuffer1;
                    device.ComputeShaderResources[2]  = rs.Sky.SkyCube;
                    device.ComputeShaderResources[4]  = hdrFrame.DepthBuffer;
                    device.ComputeShaderResources[5]  = cascadedShadowMap.ColorBuffer;
                    device.ComputeShaderResources[6]  = spotColor;
                    device.ComputeShaderResources[7]  = viewLayer.LightSet.SpotAtlas == null ? rs.WhiteTexture.Srv : viewLayer.LightSet.SpotAtlas.Texture.Srv;
                    device.ComputeShaderResources[8]  = omniLightBuffer;
                    device.ComputeShaderResources[9]  = spotLightBuffer;
                    device.ComputeShaderResources[10] = envLightBuffer;
                    device.ComputeShaderResources[11] = rs.SsaoFilter.OcclusionMap;
                    device.ComputeShaderResources[12] = viewLayer.RadianceCache;
                    device.ComputeShaderResources[13] = viewLayer.ParticleSystem.SimulatedParticles;
                    device.ComputeShaderResources[14] = cascadedShadowMap.ParticleShadow;
                    device.ComputeShaderResources[15] = envLut.Srv;
                    device.ComputeShaderResources[16] = decalBuffer;
                    device.ComputeShaderResources[17] = viewLayer.LightSet.DecalAtlas == null ? rs.WhiteTexture.Srv : viewLayer.LightSet.DecalAtlas.Texture.Srv;

                    device.ComputeShaderConstants[0] = lightingCB;

                    device.SetCSRWTexture(0, hdrFrame.LightAccumulator.Surface);
                    device.SetCSRWBuffer(1, viewLayer.ParticleSystem.ParticleLighting);

                    //
                    //	Dispatch solids :
                    //
                    using (new PixEvent("Solid Lighting")) {
                        device.PipelineState = factory[(int)LightingFlags.SOLIDLIGHTING];
                        device.Dispatch(MathUtil.IntDivUp(width, BlockSizeX), MathUtil.IntDivUp(height, BlockSizeY), 1);
                    }

                    //
                    //	Dispatch particles :
                    //
                    using (new PixEvent("Particle Lighting")) {
                        if (stereoEye != StereoEye.Right && !rs.SkipParticles)
                        {
                            int threadGroupCount = MathUtil.IntDivUp(ParticleSystem.MaxSimulatedParticles, ParticleSystem.BlockSize);
                            device.PipelineState = factory[(int)LightingFlags.PARTICLES];
                            device.Dispatch(threadGroupCount, 1, 1);
                        }
                    }
                } catch (UbershaderException e) {
                    Log.Warning("{0}", e.Message);
                }


                //
                //	Add accumulated light  :
                //
                rs.Filter.OverlayAdditive(hdrFrame.HdrBuffer.Surface, hdrFrame.LightAccumulator);

                //	Uncomment to enable SSS :
                                #if false
                rs.Filter.GaussBlur(hdrFrame.SSSAccumulator, hdrFrame.LightAccumulator, 5, 0);
                rs.Filter.OverlayAdditive(hdrFrame.HdrBuffer.Surface, hdrFrame.SSSAccumulator);
                                #endif

                device.ResetStates();


                if (rs.ShowLightCounters)
                {
                    var ls = viewLayer.LightSet;
                    Log.Message("lights: {0,5} omni {1,5} spot {2,5} env", ls.OmniLights.Count, ls.SpotLights.Count, ls.EnvLights.Count);
                }
            }
        }
示例#10
0
		/// <summary>
		/// Renders sky with specified technique
		/// </summary>
		/// <param name="rendCtxt"></param>
		/// <param name="techName"></param>
		internal void Render( Camera camera, StereoEye stereoEye, HdrFrame frame, SkySettings settings )
		{
			var scale		=	Matrix.Scaling( settings.SkySphereSize );
			var rotation	=	Matrix.Identity;

			var	sunPos		=	settings.SunPosition;
			var sunColor	=	settings.SunGlowColor;

			rs.ResetStates();

			//rs.DepthStencilState = depthBuffer==null? DepthStencilState.None : DepthStencilState.Default ;

			rs.SetTargets( frame.DepthBuffer.Surface, frame.HdrBuffer.Surface );

			var viewMatrix = camera.GetViewMatrix( stereoEye );
			var projMatrix = camera.GetProjectionMatrix( stereoEye );

			skyConstsData.MatrixWVP		= scale * rotation * MathUtil.Transformation( viewMatrix.Right, viewMatrix.Up, viewMatrix.Backward ) * projMatrix;
			skyConstsData.SunPosition	= sunPos;
			skyConstsData.SunColor		= sunColor;
			skyConstsData.Turbidity		= settings.SkyTurbidity;
			skyConstsData.Temperature	= Temperature.Get( settings.SunTemperature ); 
			skyConstsData.SkyIntensity	= settings.SkyIntensity;
	
			skyConstsCB.SetData( skyConstsData );
			
			rs.VertexShaderConstants[0] = skyConstsCB;
			rs.PixelShaderConstants[0] = skyConstsCB;


			//
			//	Sky :
			//
			SkyFlags flags = SkyFlags.SKY;

			ApplyColorSpace( ref flags, settings );
				
			rs.PipelineState	=	factory[(int)flags];
						
			rs.SetupVertexInput( skyVB, null );
			rs.Draw( skyVB.Capacity, 0 );

			rs.ResetStates();
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="gameTime"></param>
		internal void Render ( GameTime gameTime, Camera camera, StereoEye stereoEye, HdrFrame viewFrame )
		{
			var device	=	Game.GraphicsDevice;
			device.ResetStates();

			device.SetTargets( viewFrame.DepthBuffer, viewFrame.HdrBuffer );
			device.SetViewport( 0,0,viewFrame.HdrBuffer.Width, viewFrame.HdrBuffer.Height );

			int	w	=	device.DisplayBounds.Width;
			int h	=	device.DisplayBounds.Height;

			//var map	=	"SV_POSITION.xyzw;COLOR0.xyzw;COLOR1.xyzw;TEXCOORD0.xyzw;TEXCOORD1.xyzw;TEXCOORD2.xyzw";

			Params param = new Params();
			//param.View			=	Matrix.Identity;
			//param.Projection	=	Matrix.OrthoOffCenterRH(0, w, h, 0, -9999, 9999);
			param.View			=	camera.GetViewMatrix( stereoEye );
			param.Projection	=	camera.GetProjectionMatrix( stereoEye );
			param.MaxParticles	=	100;
			param.DeltaTime		=	gameTime.ElapsedSec;

			paramsCB.SetData( param );



			device.VertexShaderConstants[0]		= paramsCB ;
			device.GeometryShaderConstants[0]	= paramsCB ;
			device.PixelShaderConstants[0]		= paramsCB ;
			
			device.PixelShaderSamplers[0]		= SamplerState.LinearWrap ;


			//
			//	Simulate :
			//
			device.PipelineState	=	factory[ (int)Flags.SIMULATION ];

			device.SetupVertexInput( simulationSrcVB, null );
			device.SetupVertexOutput( simulationDstVB, 0 );
		
			device.DrawAuto();

			//
			//	Inject :
			//
			injectionVB.SetData( injectionBufferCPU );

			device.PipelineState	=	factory[ (int)Flags.INJECTION ];

			device.SetupVertexInput( injectionVB, null );
			device.SetupVertexOutput( simulationDstVB, -1 );
		
			device.Draw(injectionCount, 0 );

			SwapParticleBuffers();	

			//
			//	Render
			//
			paramsCB.SetData( param );
			device.VertexShaderConstants[0]		= paramsCB ;
			device.GeometryShaderConstants[0]	= paramsCB ;
			device.PixelShaderConstants[0]		= paramsCB ;

			device.PipelineState	=	factory[ (int)Flags.RENDER ];

			device.PixelShaderResources[0]	=	null;

			device.SetupVertexOutput( null, 0 );
			device.SetupVertexInput( simulationSrcVB, null );

			//device.Draw( Primitive.PointList, injectionCount, 0 );

			device.DrawAuto();
			//device.Draw( Primitive.PointList, MaxSimulatedParticles, 0 );


			ClearParticleBuffer();
		}
示例#12
0
		/// <summary>
		/// 
		/// </summary>
		/// <param name="view"></param>
		/// <param name="projection"></param>
		internal void RenderLighting ( StereoEye stereoEye, Camera camera, HdrFrame hdrFrame, RenderWorld viewLayer, RenderTargetCube envLight )
		{
			using ( new PixEvent("TiledLighting") ) {
				var view		=	camera.GetViewMatrix( stereoEye );
				var projection	=	camera.GetProjectionMatrix( stereoEye );

				var device = Game.GraphicsDevice;
				device.ResetStates();

				var width	=	hdrFrame.HdrBuffer.Width;
				var height	=	hdrFrame.HdrBuffer.Height;


				ICSMController csmCtrl	=	viewLayer.LightSet.DirectLight.CSMController ?? csmController;

				int activeCascadeCount	=	Math.Min( cascadedShadowMap.CascadeCount, csmCtrl.GetActiveCascadeCount() );


				//
				//	Setup compute shader parameters and states :
				//
				try {

					var cbData	=	new LightingParams();
					var invView	=	Matrix.Invert( view );
					var invVP	=	Matrix.Invert( view * projection );
					var viewPos	=	invView.TranslationVector;

					cbData.DirectLightDirection		=	new Vector4( viewLayer.LightSet.DirectLight.Direction, 0 );
					cbData.DirectLightIntensity		=	viewLayer.LightSet.DirectLight.Intensity.ToVector4();
					cbData.Projection				=	projection;

					cbData.CSMViewProjection0		=	csmCtrl.GetShadowViewMatrix(0) * csmCtrl.GetShadowProjectionMatrix(0);
					cbData.CSMViewProjection1		=	csmCtrl.GetShadowViewMatrix(1) * csmCtrl.GetShadowProjectionMatrix(1);
					cbData.CSMViewProjection2		=	csmCtrl.GetShadowViewMatrix(2) * csmCtrl.GetShadowProjectionMatrix(2);
					cbData.CSMViewProjection3		=	csmCtrl.GetShadowViewMatrix(3) * csmCtrl.GetShadowProjectionMatrix(3);

					cbData.View						=	view;
					cbData.ViewPosition				=	new Vector4(viewPos,1);
					cbData.InverseViewProjection	=	invVP;
					cbData.CSMFilterRadius			=	new Vector4( CSMFilterSize );

					cbData.AmbientColor				=	viewLayer.LightSet.AmbientLevel;
					cbData.Viewport					=	new Vector4( 0, 0, width, height );
					cbData.ShowCSLoadOmni			=	ShowOmniLightTileLoad ? 1 : 0;
					cbData.ShowCSLoadEnv			=	ShowEnvLightTileLoad  ? 1 : 0;
					cbData.ShowCSLoadSpot			=	ShowSpotLightTileLoad ? 1 : 0;

					cbData.CascadeCount				=	activeCascadeCount;
					cbData.CascadeScale				=	1.0f / (float)cascadedShadowMap.CascadeCount;


					ComputeOmniLightsTiles( view, projection, viewLayer.LightSet );
					ComputeSpotLightsTiles( view, projection, viewLayer.LightSet );
					ComputeEnvLightsTiles(  view, projection, viewLayer.LightSet );

					//
					//	set states :
					//
					device.SetTargets( null, hdrFrame.HdrBuffer.Surface );

					lightingCB.SetData( cbData );

					device.ComputeShaderSamplers[0]	=	SamplerState.PointClamp;
					device.ComputeShaderSamplers[1]	=	SamplerState.LinearClamp;
					device.ComputeShaderSamplers[2]	=	SamplerState.ShadowSampler;
					device.ComputeShaderSamplers[3]	=	SamplerState.LinearPointWrap;

					device.ComputeShaderResources[0]	=	hdrFrame.DiffuseBuffer;
					device.ComputeShaderResources[1]	=	hdrFrame.SpecularBuffer;
					device.ComputeShaderResources[2]	=	hdrFrame.NormalMapBuffer;
					device.ComputeShaderResources[3]	=	hdrFrame.ScatteringBuffer;
					device.ComputeShaderResources[4]	=	hdrFrame.DepthBuffer;
					device.ComputeShaderResources[5]	=	cascadedShadowMap.ColorBuffer;
					device.ComputeShaderResources[6]	=	spotColor;
					device.ComputeShaderResources[7]	=	viewLayer.LightSet.SpotAtlas==null ? rs.WhiteTexture.Srv : viewLayer.LightSet.SpotAtlas.Texture.Srv;
					device.ComputeShaderResources[8]	=	omniLightBuffer;
					device.ComputeShaderResources[9]	=	spotLightBuffer;
					device.ComputeShaderResources[10]	=	envLightBuffer;
					device.ComputeShaderResources[11]	=	rs.SsaoFilter.OcclusionMap;
					device.ComputeShaderResources[12]	=	viewLayer.RadianceCache;
					device.ComputeShaderResources[13]	=	viewLayer.ParticleSystem.SimulatedParticles;
					device.ComputeShaderResources[14]	=	cascadedShadowMap.ParticleShadow;

					device.ComputeShaderConstants[0]	=	lightingCB;

					device.SetCSRWTexture( 0, hdrFrame.LightAccumulator.Surface );
					device.SetCSRWTexture( 1, hdrFrame.SSSAccumulator.Surface );
					device.SetCSRWBuffer(  2, viewLayer.ParticleSystem.ParticleLighting );

					//
					//	Dispatch solids :
					//
					using (new PixEvent("Solid Lighting")) {
						device.PipelineState	=	factory[ (int)LightingFlags.SOLIDLIGHTING ];
						device.Dispatch( MathUtil.IntDivUp( width, BlockSizeX ), MathUtil.IntDivUp( height, BlockSizeY ), 1 );
					}

					//
					//	Dispatch particles :
					//
					using (new PixEvent("Particle Lighting")) {
						if (stereoEye!=StereoEye.Right) {
							int threadGroupCount	=	MathUtil.IntDivUp( ParticleSystem.MaxSimulatedParticles, ParticleSystem.BlockSize );
							device.PipelineState	=	factory[ (int)LightingFlags.PARTICLES ];
							device.Dispatch( threadGroupCount, 1, 1 );
						}
					}
	
				} catch ( UbershaderException e ) {
					Log.Warning("{0}", e.Message );
				}


				//
				//	Add accumulated light  :
				//
				rs.Filter.OverlayAdditive( hdrFrame.HdrBuffer.Surface, hdrFrame.LightAccumulator );

				//	Uncomment to enable SSS :
				#if false
				rs.Filter.GaussBlur( hdrFrame.SSSAccumulator, hdrFrame.LightAccumulator, 5, 0 ); 
				rs.Filter.OverlayAdditive( hdrFrame.HdrBuffer.Surface, hdrFrame.SSSAccumulator );
				#endif

				device.ResetStates();


				if (rs.ShowLightCounters) {
					var ls = viewLayer.LightSet;
					Log.Message("lights: {0,5} omni {1,5} spot {2,5} env", ls.OmniLights.Count, ls.SpotLights.Count, ls.EnvLights.Count );
				}
			}
		}
示例#13
0
		/// <summary>
		/// Creates ViewLayerHDR instance
		/// </summary>
		/// <param name="Game">Game engine</param>
		/// <param name="width">Target width.</param>
		/// <param name="height">Target height.</param>
		public RenderWorld ( Game game, int width, int height ) : base( game )
		{
			var vp	=	Game.GraphicsDevice.DisplayBounds;

			if (width<=0) {
				width	=	vp.Width;
			}
			if (height<=0) {
				height	=	vp.Height;
			}

			HdrSettings		=	new HdrSettings();
			SkySettings		=	new SkySettings();
			DofSettings		=	new DofSettings();

			Instances		=	new List<MeshInstance>();
			LightSet		=	new LightSet( Game.RenderSystem );

			debug			=	new DebugRender( Game );
			
			particleSystem	=	new ParticleSystem( Game.RenderSystem, this );

			MeasuredOld		=	new RenderTarget2D( Game.GraphicsDevice, ColorFormat.Rgba32F,   1,  1 );
			MeasuredNew		=	new RenderTarget2D( Game.GraphicsDevice, ColorFormat.Rgba32F,   1,  1 );

			radianceFrame	=	new HdrFrame( Game, 512,512 );

			Radiance		=	new RenderTargetCube( Game.GraphicsDevice, ColorFormat.Rgba16F, RenderSystem.EnvMapSize, true );
			RadianceCache	=	new TextureCubeArray( Game.GraphicsDevice, 128, RenderSystem.MaxEnvLights, ColorFormat.Rgba16F, true );

			Resize( width, height );
		}
示例#14
0
		/// <summary>
		/// 
		/// </summary>
		void ClearBuffers ( HdrFrame frame )
		{
			Game.GraphicsDevice.Clear( frame.DiffuseBuffer.Surface,		Color4.Black );
			Game.GraphicsDevice.Clear( frame.SpecularBuffer.Surface,	Color4.Black );
			Game.GraphicsDevice.Clear( frame.NormalMapBuffer.Surface,	Color4.Black );
			Game.GraphicsDevice.Clear( frame.ScatteringBuffer.Surface,	Color4.Black );

			Game.GraphicsDevice.Clear( frame.DepthBuffer.Surface,		1, 0 );
			Game.GraphicsDevice.Clear( frame.HdrBuffer.Surface,			Color4.Black );
		}
示例#15
0
		/// <summary>
		/// 
		/// </summary>
		/// <param name="width"></param>
		/// <param name="height"></param>
		public void Resize ( int newWidth, int newHeight )
		{
			SafeDispose( ref viewHdrFrame );

			SafeDispose( ref Bloom0 );
			SafeDispose( ref Bloom1 );

			//	clamp values :
			newWidth	=	Math.Max(128, newWidth);
			newHeight	=	Math.Max(128, newHeight);

			int targetWidth		=	newWidth;
			int targetHeight	=	newHeight;

			int bloomWidth		=	( targetWidth/2  ) & 0xFFF0;
			int bloomHeight		=	( targetHeight/2 ) & 0xFFF0;

			viewHdrFrame		=	new HdrFrame ( Game, targetWidth, targetHeight );
			
			Bloom0				=	new RenderTarget2D( Game.GraphicsDevice, ColorFormat.Rgba16F, bloomWidth, bloomHeight, true, false );
			Bloom1				=	new RenderTarget2D( Game.GraphicsDevice, ColorFormat.Rgba16F, bloomWidth, bloomHeight, true, false );

			//HdrTexture			=	new TargetTexture( HdrBuffer );
			//DiffuseTexture		=	new TargetTexture( DiffuseBuffer );
			//SpecularTexture		=	new TargetTexture( SpecularBuffer );
			//NormalMapTexture	=	new TargetTexture( NormalMapBuffer );
		}