Exemple #1
0
		/// <summary>
		/// Creates texture atlas from stream.
		/// </summary>
		/// <param name="device"></param>
		public TextureAtlas ( GraphicsDevice device, Stream stream, bool useSRgb = false )
		{
			using ( var br = new BinaryReader(stream) ) {
			
				if (!br.CheckMagic("ATLS")) {
					throw new IOException("Bad texture atlas file");
				}
				
				int count = br.ReadInt32();
				
				for ( int i=0; i<count; i++ ) {
					var element = new Element();
					element.Name	=	br.ReadString();
					element.X		=	br.ReadInt32();
					element.Y		=	br.ReadInt32();
					element.Width	=	br.ReadInt32();
					element.Height	=	br.ReadInt32();

					elements.Add( element );
				}				

				int ddsFileLength	=	br.ReadInt32();
				
				var ddsImageBytes	=	br.ReadBytes( ddsFileLength );

				texture	=	new Texture2D( device, ddsImageBytes, useSRgb );
			}


			dictionary	=	elements.ToDictionary( e => e.Name );
		}
Exemple #2
0
        /// <summary>
        /// Creates render target
        /// </summary>
        /// <param name="rs"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="format"></param>
        public DepthStencilCube( GraphicsDevice device, DepthFormat format, int size, int samples, string debugName = "" )
            : base(device)
        {
            bool msaa	=	samples > 1;

            CheckSamplesCount( samples );

            SampleCount	=	samples;

            Format		=	format;
            SampleCount	=	samples;
            Width		=	size;
            Height		=	size;
            Depth		=	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.ConvertToTex( format );
                texDesc.MipLevels			=	1;
                texDesc.OptionFlags			=	ResourceOptionFlags.TextureCube;
                texDesc.SampleDescription	=	new DXGI.SampleDescription(samples, 0);
                texDesc.Usage				=	ResourceUsage.Default;

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

            var srvDesc	=	new ShaderResourceViewDescription();
                srvDesc.Dimension			=	samples > 1 ? ShaderResourceViewDimension.Texture2DMultisampled : ShaderResourceViewDimension.Texture2D;
                srvDesc.Format				=	Converter.ConvertToSRV( format );
                srvDesc.Texture2D.MostDetailedMip	=	0;
                srvDesc.Texture2D.MipLevels			=	1;

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

            //
            //	Create surfaces :
            //
            surfaces	=	new DepthStencilSurface[ 6 ];

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

                var rtvDesc = new DepthStencilViewDescription();
                    rtvDesc.Texture2DArray.MipSlice			=	0;
                    rtvDesc.Texture2DArray.FirstArraySlice	=	face;
                    rtvDesc.Texture2DArray.ArraySize		=	1;
                    rtvDesc.Dimension						=	msaa ? DepthStencilViewDimension.Texture2DMultisampledArray : DepthStencilViewDimension.Texture2DArray;
                    rtvDesc.Format							=	Converter.ConvertToDSV( format );

                var dsv	=	new DepthStencilView( device.Device, texCube, rtvDesc );

                int subResId	=	Resource.CalculateSubResourceIndex( 0, face, 1 );

                surfaces[face]	=	new DepthStencilSurface( dsv, format, Width, Height, SampleCount );
            }
        }
Exemple #3
0
        /// <summary>
        /// Creates depth stencil texture, view and shader resource with format D24S8
        /// </summary>
        /// <param name="rs"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="format"></param>
        public DepthStencil2D( GraphicsDevice device, DepthFormat format, int width, int height, int samples = 1 )
            : base(device)
        {
            CheckSamplesCount( samples );

            Width		=	width;
            Height		=	height;
            Depth		=	1;
            Format		=	format;
            SampleCount	=	samples;

            var bindFlags	=	BindFlags.DepthStencil;

            if (device.GraphicsProfile==GraphicsProfile.HiDef) {
                bindFlags	|=	BindFlags.ShaderResource;

            } else if (device.GraphicsProfile==GraphicsProfile.Reach) {
                if (samples==1) {
                    bindFlags	|=	BindFlags.ShaderResource;
                }
            }

            var	texDesc	=	new Texture2DDescription();
                texDesc.Width				=	width;
                texDesc.Height				=	height;
                texDesc.ArraySize			=	1;
                texDesc.BindFlags			=	bindFlags;
                texDesc.CpuAccessFlags		=	CpuAccessFlags.None;
                texDesc.Format				=	Converter.ConvertToTex( format );
                texDesc.MipLevels			=	1;
                texDesc.OptionFlags			=	ResourceOptionFlags.None;
                texDesc.SampleDescription	=	new DXGI.SampleDescription(samples, 0);
                texDesc.Usage				=	ResourceUsage.Default;

            var dsvDesc	=	new DepthStencilViewDescription();
                dsvDesc.Dimension			=	samples > 1 ? DepthStencilViewDimension.Texture2DMultisampled : DepthStencilViewDimension.Texture2D;
                dsvDesc.Format				=	Converter.ConvertToDSV( format );
                dsvDesc.Flags				=	DepthStencilViewFlags.None;

            var srvDesc	=	new ShaderResourceViewDescription();
                srvDesc.Dimension			=	samples > 1 ? ShaderResourceViewDimension.Texture2DMultisampled : ShaderResourceViewDimension.Texture2D;
                srvDesc.Format				=	Converter.ConvertToSRV( format );
                srvDesc.Texture2D.MostDetailedMip	=	0;
                srvDesc.Texture2D.MipLevels			=	1;

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

            var dsv		=	new DepthStencilView	( device.Device, tex2D,	dsvDesc );

            if (bindFlags.HasFlag( BindFlags.ShaderResource)) {
                SRV		=	new ShaderResourceView	( device.Device, tex2D,	srvDesc );
            }

            surface		=	new DepthStencilSurface	( dsv, format, width, height, samples );
        }
Exemple #4
0
        /// <summary>
        /// 
        /// </summary>
        public PipelineState( GraphicsDevice device )
            : base(device)
        {
            BlendState			=	BlendState.Opaque;
            RasterizerState		=	RasterizerState.CullNone;
            DepthStencilState	=	DepthStencilState.Default;
            RasterizedStream	=	-1;
            Primitive			=	Primitive.TriangleList;

            isReady		=	false;
        }
Exemple #5
0
        /// <summary>
        /// Creates an instance of this object.
        /// </summary>
        /// <param name="device"></param>
        /// <param name="capacity"></param>
        public IndexBuffer( GraphicsDevice device, int capacity )
        {
            //Log.Message("Creation: Index Buffer");

            this.device		=	device;
            this.capacity	=	capacity;

            BufferDescription	desc = new BufferDescription();

            desc.BindFlags				=	BindFlags.IndexBuffer;
            desc.CpuAccessFlags			=	CpuAccessFlags.Write;
            desc.OptionFlags			=	ResourceOptionFlags.None;
            desc.SizeInBytes			=	capacity * sizeof(int);
            desc.StructureByteStride	=	0;
            desc.Usage					=	ResourceUsage.Dynamic;

            lock (device.DeviceContext) {
                indexBuffer	=	new D3D11.Buffer( device.Device, desc );
            }
        }
Exemple #6
0
 /// <summary>
 /// Creates render target
 /// </summary>
 /// <param name="rs"></param>
 /// <param name="width"></param>
 /// <param name="height"></param>
 /// <param name="format"></param>
 public RenderTargetCube( GraphicsDevice device, ColorFormat format, int size, int samples, string debugName = "" )
     : base(device)
 {
     Create( format, size, samples, false, debugName );
 }
Exemple #7
0
 public Filter( Game game )
     : base(game)
 {
     rs = game.GraphicsDevice;
 }
Exemple #8
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="device"></param>
        internal void Apply( GraphicsDevice device )
        {
            if ( state == null ) {

                var dss	=	new DepthStencilStateDescription();

                dss.DepthComparison		=	Converter.Convert( this.depthComparison );
                dss.DepthWriteMask		=	this.depthWriteEnabled ? DepthWriteMask.All : DepthWriteMask.Zero;
                dss.IsDepthEnabled		=	this.depthEnabled;
                dss.IsStencilEnabled	=	this.stencilEnabled;
                dss.StencilReadMask		=	this.stencilReadMask;
                dss.StencilWriteMask	=	this.stencilWriteMask;

                dss.BackFace.Comparison				=	Converter.Convert( this.backStencilComparison	);
                dss.BackFace.FailOperation			=	Converter.Convert( this.backFailOp				);
                dss.BackFace.DepthFailOperation		=	Converter.Convert( this.backDepthFailOp			);
                dss.BackFace.PassOperation			=	Converter.Convert( this.backPassOp				);

                dss.FrontFace.Comparison			=	Converter.Convert( this.backStencilComparison	);
                dss.FrontFace.FailOperation			=	Converter.Convert( this.backFailOp				);
                dss.FrontFace.DepthFailOperation	=	Converter.Convert( this.backDepthFailOp			);
                dss.FrontFace.PassOperation			=	Converter.Convert( this.backPassOp				);

                state	=	new D3DDepthStencilState( device.Device, dss );
            }

            device.DeviceContext.OutputMerger.DepthStencilState		=	state;
            device.DeviceContext.OutputMerger.DepthStencilReference	=	stencilReference;
        }
 /// <summary>
 /// Creates instance of sampler state collection.
 /// </summary>
 /// <param name="device"></param>
 internal SamplerStateCollection( GraphicsDevice device, CommonShaderStage stage )
     : base(device)
 {
     states		=	new SamplerState[ Count ];
     this.stage	=	stage;
 }
Exemple #10
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="device"></param>
 public GraphicsResource( GraphicsDevice device )
 {
     this.device	=	device;
 }
Exemple #11
0
        void render(GraphicsDevice device, LayoutSystem ls, Params parameters, GameTime gameTime)
        {
            parameters.MaxParticles	= lay.ParticleCount;
            parameters.edgeOpacity	= Config.EdgeOpacity;
            parameters.nodeScale	= Config.NodeScale;
            parameters.highNodeColor = HighlightNodeColor;
            parameters.highEdgeColor = HighlightEdgeColor;

            device.ResetStates();
            device.ClearBackbuffer( BackgroundColor );
            device.SetTargets( null, device.BackbufferColor );
            paramsCB.SetData(parameters);

            device.ComputeShaderConstants	[0] = paramsCB;
            device.VertexShaderConstants	[0] = paramsCB;
            device.GeometryShaderConstants	[0] = paramsCB;
            device.PixelShaderConstants		[0] = paramsCB;

            device.PixelShaderSamplers		[0] = SamplerState.LinearWrap;

            int anchorFlag = (AnchorToNodes ? (int)RenderFlags.RELATIVE_POS : (int)RenderFlags.ABSOLUTE_POS);

            // draw points: ---------------------------------------------------------------------------
            device.PipelineState = factory[(int)RenderFlags.DRAW|(int)RenderFlags.POINT|anchorFlag];
            device.SetCSRWBuffer( 0, null );
            device.GeometryShaderResources	[2] = ls.CurrentStateBuffer;

            //			device.PixelShaderResources		[0] = particleTex;
            device.PixelShaderResources		[0] = atlas.Texture;
            device.Draw(nodeList.Count, 0);

            // draw lines: ----------------------------------------------------------------------------
            device.PipelineState = factory[(int)RenderFlags.DRAW|(int)RenderFlags.LINE|anchorFlag];
            device.GeometryShaderResources	[2] = ls.CurrentStateBuffer;
            device.GeometryShaderResources	[3] = ls.LinksBuffer;
            device.Draw( edgeList.Count, 0 );

            // draw highlighted points: ---------------------------------------------------------------
            device.PipelineState = factory[(int)RenderFlags.DRAW | (int)RenderFlags.SELECTION|anchorFlag];
            device.PixelShaderResources		[0] = highlightTex;

            foreach (var high in highlightNodesList)
            {
                device.GeometryShaderResources[4] = high.Item1;
                parameters.highNodeColor = high.Item2.color.ToVector4();
                paramsCB.SetData(parameters);
                int num = high.Item2.number;
                device.Draw(num, 0);
            }

            // draw highlighted lines: ----------------------------------------------------------------
            //		device.PipelineState = factory[(int)RenderFlags.DRAW | (int)RenderFlags.HIGH_LINE|anchorFlag];
            //		device.GeometryShaderResources	[5] = highlightedEdgesBuffer;
            //		device.Draw(highlightedEdgesBuffer.GetStructureCount(), 0);

            // draw sparks: ---------------------------------------------------------------------------
            List<Spark> updSparks = new List<Spark>();
            foreach (var sp in sparkList)
            {
                Spark updSp = sp;
                updSp.Parameter = sp.Parameter + gameTime.Elapsed.Milliseconds / sp.Time;
                if (updSp.Parameter < 1.0f)
                {
                    updSparks.Add(updSp);
                }
            }
            sparkList = updSparks;

            if (sparkBuffer != null && updSparks.Count > 0)
            {
                sparkBuffer.SetData(updSparks.ToArray());

                device.PipelineState = factory[(int)RenderFlags.DRAW | (int)RenderFlags.SPARKS | anchorFlag];
                device.PixelShaderResources		[0] = sparkTex;
                device.GeometryShaderResources	[2] = ls.CurrentStateBuffer;
                device.GeometryShaderResources	[6] = sparkBuffer;
                device.Draw(sparkList.Count, 0);
            }
        }
Exemple #12
0
		/// <summary>
		/// 
		/// </summary>
		/// <param name="device"></param>
		protected ShaderResource( GraphicsDevice device ) : base(device)
		{
		}
Exemple #13
0
 internal ComputeShader( GraphicsDevice device, string bytecode )
     : base(bytecode)
 {
     Shader = new D3D11.ComputeShader( device.Device, Misc.HexStringToByte(bytecode) );
 }
Exemple #14
0
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="rs"></param>
		public Sky ( Game game ) : base( game )
		{
			rs	=	game.GraphicsDevice;
			Params = new Config();
		}
Exemple #15
0
        void render( GraphicsDevice device, LayoutSystem ls, Params parameters )
        {
            parameters.MaxParticles	= lay.ParticleCount;
            parameters.edgeOpacity	= Config.EdgeOpacity;
            parameters.nodeScale	= Config.NodeScale;
            parameters.highNodeColor = HighlightNodeColor;
            parameters.highEdgeColor = HighlightEdgeColor;

            device.ResetStates();
            device.ClearBackbuffer(Color.Black); //background
            device.SetTargets( null, device.BackbufferColor );
            paramsCB.SetData(parameters);

            device.ComputeShaderConstants	[0] = paramsCB;
            device.VertexShaderConstants	[0] = paramsCB;
            device.GeometryShaderConstants	[0] = paramsCB;
            device.PixelShaderConstants		[0] = paramsCB;

            device.PixelShaderSamplers		[0] = SamplerState.LinearWrap;

            // draw points: ------------------------------------------------------------------------
            device.PipelineState = factory[(int)RenderFlags.DRAW|(int)RenderFlags.POINT];
            device.SetCSRWBuffer( 0, null );
            device.PixelShaderResources		[0] = particleTex;
            device.GeometryShaderResources	[2] = ls.CurrentStateBuffer;
            device.Draw( nodeList.Count, 0 );

            // draw lines: -------------------------------------------------------------------------
            device.PipelineState = factory[(int)RenderFlags.DRAW|(int)RenderFlags.LINE];
            device.GeometryShaderResources	[2] = ls.CurrentStateBuffer;
            device.GeometryShaderResources	[3] = ls.LinksBuffer;
            device.Draw( edgeList.Count, 0 );

            // draw selected points: ---------------------------------------------------------------
            device.PipelineState = factory[(int)RenderFlags.DRAW | (int)RenderFlags.SELECTION];
            device.PixelShaderResources		[1] = selectionTex;
            device.GeometryShaderResources	[4] = selectedNodesBuffer;
            device.Draw(numSelectedNodes, 0);

            // draw selected lines: ----------------------------------------------------------------
            device.PipelineState = factory[(int)RenderFlags.DRAW | (int)RenderFlags.HIGH_LINE];
            device.GeometryShaderResources	[5] = selectedEdgesBuffer;
            device.Draw(numSelectedEdges, 0);
        }
 /// <summary>
 /// Creates instance of sampler state collection.
 /// </summary>
 /// <param name="device"></param>
 internal ConstantBufferCollection( GraphicsDevice device, CommonShaderStage stage )
     : base(device)
 {
     buffers		=	new ConstantBuffer[ Count ];
     this.stage	=	stage;
 }
Exemple #17
0
 /// <summary>
 /// Creates buffer from given indices
 /// </summary>
 /// <param name="device"></param>
 /// <param name="indices"></param>
 /// <returns></returns>
 public static IndexBuffer Create( GraphicsDevice device, int[] indices )
 {
     var ib = new IndexBuffer( device, indices.Length );
     ib.SetData( indices );
     return ib;
 }
Exemple #18
0
        /// <summary>
        /// Constrcutor
        /// </summary>
        /// <param name="device"></param>
        /// <param name="fileName"></param>
        public SpriteFont( GraphicsDevice rs, Stream stream )
        {
            this.rs	=	rs;

            FontFile input = FontLoader.Load( stream );

            int numGlyphs	=	input.Chars.Max( ch => ch.ID );

            //	create charInfo and kernings :
            fontInfo.kernings = new Dictionary<Tuple<char,char>, float>();
            fontInfo.charInfo = new SpriteFontInfo.CharInfo[numGlyphs+1];

            //	check UNICODE :
            /*if (input.Info.Unicode!=0) {
                throw new SystemException("UNICODE characters are not supported (Remove UNICODE flag)");
            } */

            //	check one-page bitmap fonts :
            if (input.Pages.Count!=1) {
                throw new GraphicsException("Only one page of font image is supported");
            }

            //	create path for font-image :
            string fontImagePath	=	input.Pages[0].File;

            fontTexture				=	rs.Game.Content.Load<Texture2D>( fontImagePath );

            //	Fill structure :
            fontInfo.fontFace		=	input.Info.Face;
            fontInfo.baseLine		=	input.Common.Base;
            fontInfo.lineHeight		=	input.Common.LineHeight;
            fontInfo.scaleWidth		=	input.Common.ScaleW;
            fontInfo.scaleHeight	=	input.Common.ScaleH;

            float scaleWidth = fontInfo.scaleWidth;
            float scaleHeight = fontInfo.scaleHeight;

            //	process character info :
            for ( int i=0; i<input.Chars.Count; i++) {
                FontChar ch = input.Chars[i];

                int id = ch.ID;
                int x = ch.X;
                int y = ch.Y;
                int xoffs = ch.XOffset;
                int yoffs = ch.YOffset;
                int w = ch.Width;
                int h = ch.Height;

                fontInfo.charInfo[ ch.ID ].validChar	=	true;
                fontInfo.charInfo[ ch.ID ].xAdvance		=	ch.XAdvance;
                fontInfo.charInfo[ ch.ID ].srcRect		=	new RectangleF(x, y, w, h);
                fontInfo.charInfo[ ch.ID ].dstRect		=	new RectangleF(xoffs, yoffs, w, h);
            }

            var letterHeights = input.Chars
                    .Where( ch1 => char.IsUpper( (char)(ch1.ID) ) )
                    .Select( ch2 => ch2.Height )
                    .OrderBy( h => h )
                    .ToList();
            CapHeight	=	letterHeights[ letterHeights.Count/2 ];

            //	process kerning info :
            for ( int i=0; i<input.Kernings.Count; i++) {
                var pair	=	new Tuple<char,char>( (char)input.Kernings[i].First, (char)input.Kernings[i].Second);
                int kerning =	input.Kernings[i].Amount;
                fontInfo.kernings.Add( pair, kerning );
            }

            SpaceWidth	=	MeasureString(" ").Width;
            LineHeight	=	MeasureString(" ").Height;
        }
Exemple #19
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="device"></param>
 public GraphicsResource(GraphicsDevice device)
 {
     this.device = device;
 }
Exemple #20
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="device"></param>
 public SpriteBatch( Game game )
     : base(game)
 {
     Cfg				=	new Config();
     this.device		=	Game.GraphicsDevice;
 }
Exemple #21
0
 public ShaderFactory( GraphicsDevice device )
 {
     this.device	=	device;
 }
Exemple #22
0
 /// <summary>
 /// Creates render target
 /// </summary>
 /// <param name="rs"></param>
 /// <param name="width"></param>
 /// <param name="height"></param>
 /// <param name="format"></param>
 public RenderTargetCube( GraphicsDevice device, ColorFormat format, int size, bool mips, string debugName = "" )
     : base(device)
 {
     Create( format, size, 1, mips, debugName );
 }
Exemple #23
0
 internal GeometryShader( GraphicsDevice device, string bytecode )
     : base(bytecode)
 {
     Shader = new D3D11.GeometryShader( device.Device, Misc.HexStringToByte(bytecode) );
 }
Exemple #24
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="device"></param>
        internal D3DSamplerState Apply( GraphicsDevice device )
        {
            if ( state == null ) {

                var ssd = new SamplerStateDescription();

                ssd.ComparisonFunction	=	Converter.Convert( this.compareFunc );
                ssd.AddressU			=	Converter.Convert( this.addressU );
                ssd.AddressV			=	Converter.Convert( this.addressV );
                ssd.AddressW			=	Converter.Convert( this.addressW );
                ssd.BorderColor			=	SharpDXHelper.Convert( this.borderColor );
                ssd.Filter				=	Converter.Convert( this.filter );
                ssd.MaximumAnisotropy	=	this.maxAnisotropy;
                ssd.MaximumLod			=	this.maxMipLevel;
                ssd.MinimumLod			=	this.minMipLevel;
                ssd.MipLodBias			=	this.mipMapBias;

                state	=	new D3DSamplerState( device.Device, ssd );
            }

            return state;
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="device"></param>
 internal ShaderResourceCollection( GraphicsDevice device, CommonShaderStage stage )
     : base(device)
 {
     resources	=	new ShaderResource[ Count ];
     this.stage	=	stage;
 }
Exemple #26
0
        // Constructor: ----------------------------------------------------------------------------------------
        public LayoutSystem(Game game, Ubershader ubershader)
        {
            env = game;
            shader = ubershader;
            device = env.GraphicsDevice;
            factory = new StateFactory(shader, typeof(ComputeFlags), (plState, comb) =>
            {
                plState.RasterizerState = RasterizerState.CullNone;
                plState.BlendState = BlendState.AlphaBlend; //BlendState.NegMultiply;
                plState.DepthStencilState = DepthStencilState.Readonly;
                plState.Primitive = Primitive.PointList;
            });

            paramsCB = new ConstantBuffer(env.GraphicsDevice, typeof(ComputeParams));

            categories		= new List<Category>();
            categoryIndices	= new List<int>();

            SpringTension = env.GetService<GraphSystem>().Config.SpringTension;
            StepMode	= env.GetService<GraphSystem>().Config.StepMode;
            calcAuto	= new CalculationSystemAuto(this);
            calcFixed	= new CalculationSystemFixed(this);
            calcWolfram = new CalculationSystemWolfram(this);
        }