• Advertisement
Sign in to follow this  

How many draw calls per frame are possible without awful performance?

This topic is 1779 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have just completed attempts to write my own sprite batch class in C# using SlimDX, in direct response to performance problems in my game.

 

Basically I draw the game area (pacman) in tiles, with each tile drawn using a draw call to the sprite renderer object. I am now using a decent sprite renderer (http://sdxspritetext.codeplex.com/wikipage?title=Concepts%20of%20the%20SpriteRenderer) but find that my performance problem remains the same!

 

Basically I was fine with a 10x10 tile game area, but after scaling up to 30x15, the performance is terrble. Literally 5 frames per second.

 

How should I be handling the drawing of game tiles in this scenario? Or could there be something else wrong with the way in which I've got things set up?

 

My graphics device set-up code is as follows (excuse the mess):

 

		// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		// START: Method InitialiseGraphics
		// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		public void InitialiseGraphics()
		{
			bool debug=false;
			
			if(debug){MessageBox.Show( "InitialiseGraphics()...");}
			
			description = new SlimDX.DXGI.SwapChainDescription()
            {
                BufferCount = 2,
                Usage = SlimDX.DXGI.Usage.RenderTargetOutput,
                OutputHandle = this.Handle,
                IsWindowed = true,
                ModeDescription = new SlimDX.DXGI.ModeDescription(0, 0, new SlimDX.Rational(60, 1), SlimDX.DXGI.Format.R8G8B8A8_UNorm),
                SampleDescription = new SlimDX.DXGI.SampleDescription(1, 0),
                Flags = SlimDX.DXGI.SwapChainFlags.AllowModeSwitch,
                SwapEffect = SlimDX.DXGI.SwapEffect.Discard
            };
		
			if(debug){MessageBox.Show( "InitialiseGraphics()...1");}
			
			SlimDX.Direct3D11.FeatureLevel[] featureLevels = new SlimDX.Direct3D11.FeatureLevel[] { SlimDX.Direct3D11.FeatureLevel.Level_10_1 };

			if(debug){MessageBox.Show( "InitialiseGraphics()...2");}
	
			// 02/09/12: Change constructor to enable text writing!
			SlimDX.Direct3D11.Device.CreateWithSwapChain(
				adapter1, 
				SlimDX.Direct3D11.DeviceCreationFlags.Debug, 
				featureLevels,
				description, 
				out graphics, 
				out swapChain
			);	
			
			if(debug){MessageBox.Show( "InitialiseGraphics()...3");}
			
			// create a view of our render target, which is the backbuffer of the swap chain we just created
			using (var resource = SlimDX.Direct3D11.Resource.FromSwapChain<SlimDX.Direct3D11.Texture2D>(swapChain, 0))
                renderTarget = new SlimDX.Direct3D11.RenderTargetView(graphics, resource);

				if(debug){MessageBox.Show( "InitialiseGraphics()...4");}
				
			// setting a viewport is required if you want to actually see anything
            context = graphics.ImmediateContext;
            var viewport = new SlimDX.Direct3D11.Viewport(0.0f, 0.0f, this.ClientSize.Width, this.ClientSize.Height);
            context.OutputMerger.SetTargets(renderTarget);
            context.Rasterizer.SetViewports(viewport);
			
			if(debug){MessageBox.Show( "InitialiseGraphics()...5");}
			
			bsd = new SlimDX.Direct3D11.BlendStateDescription();
			bsd.RenderTargets[0].BlendEnable = true;
			bsd.RenderTargets[0].SourceBlend = SlimDX.Direct3D11.BlendOption.SourceAlpha;
			bsd.RenderTargets[0].DestinationBlend = SlimDX.Direct3D11.BlendOption.InverseSourceAlpha;
			bsd.RenderTargets[0].BlendOperation = SlimDX.Direct3D11.BlendOperation.Add;
			bsd.RenderTargets[0].SourceBlendAlpha = SlimDX.Direct3D11.BlendOption.One;
			bsd.RenderTargets[0].DestinationBlendAlpha = SlimDX.Direct3D11.BlendOption.Zero;
			bsd.RenderTargets[0].BlendOperationAlpha = SlimDX.Direct3D11.BlendOperation.Add;
			bsd.RenderTargets[0].RenderTargetWriteMask = SlimDX.Direct3D11.ColorWriteMaskFlags.All;
			BlendState_Transparent = SlimDX.Direct3D11.BlendState.FromDescription(graphics, bsd);
			
			context.OutputMerger.BlendState = BlendState_Transparent;
			
			// ... END
			// ---------------------------------------------------------------------------------------------------------------------------
			
			if(debug){MessageBox.Show( "InitialiseGraphics()...5.5");}
			
			SlimDX.Direct3D11.RasterizerStateDescription rasterizerStateDescription = new SlimDX.Direct3D11.RasterizerStateDescription 
			{
				CullMode = SlimDX.Direct3D11.CullMode.None, 
				FillMode = SlimDX.Direct3D11.FillMode.Solid
			};
 
			context.Rasterizer.State = SlimDX.Direct3D11.RasterizerState.FromDescription(graphics, rasterizerStateDescription);
			
			if(debug){MessageBox.Show( "InitialiseGraphics()...6");}

			// load and compile the vertex shader (from the FX file)
            using (var bytecode = SlimDX.D3DCompiler.ShaderBytecode.CompileFromFile("D:\\Programming\\SharpDevelop_Projects\\SlimDX_StartOver\\BasicWindow_1\\BasicWindow_1\\triangle8.fx", "TextVS", "vs_4_0", SlimDX.D3DCompiler.ShaderFlags.None, SlimDX.D3DCompiler.EffectFlags.None))
            {
                inputSignature = SlimDX.D3DCompiler.ShaderSignature.GetInputSignature(bytecode);
                vertexShader = new SlimDX.Direct3D11.VertexShader(graphics, bytecode);
            } 
			
			if(debug){MessageBox.Show( "InitialiseGraphics()...7");}
			
			// load and compile the pixel shader (from the FX file)
            using (var bytecode = SlimDX.D3DCompiler.ShaderBytecode.CompileFromFile("D:\\Programming\\SharpDevelop_Projects\\SlimDX_StartOver\\BasicWindow_1\\BasicWindow_1\\triangle8.fx", "TextPS", "ps_4_0", SlimDX.D3DCompiler.ShaderFlags.None, SlimDX.D3DCompiler.EffectFlags.None))
                pixelShader = new SlimDX.Direct3D11.PixelShader(graphics, bytecode);
				
			if(debug){MessageBox.Show( "InitialiseGraphics()... TEXTURE CODE....2");}
			// SamplerDescription
			// GT 17/07/12: Check this against the DX11 book!!!!
			// 
			samplerDescription = new SlimDX.Direct3D11.SamplerDescription();
            samplerDescription.AddressU = SlimDX.Direct3D11.TextureAddressMode.Wrap;
            samplerDescription.AddressV = SlimDX.Direct3D11.TextureAddressMode.Wrap;
            samplerDescription.AddressW = SlimDX.Direct3D11.TextureAddressMode.Wrap;
			samplerDescription.ComparisonFunction = SlimDX.Direct3D11.Comparison.Never;
			samplerDescription.Filter = SlimDX.Direct3D11.Filter.MinPointMagMipLinear;
					
			if(debug){MessageBox.Show( "InitialiseGraphics()... TEXTURE CODE....3");}
			// SamplerState
			SlimDX.Direct3D11.SamplerState samplerState = SlimDX.Direct3D11.SamplerState.FromDescription(graphics, samplerDescription);
			
			if(debug){MessageBox.Show( "InitialiseGraphics()... TEXTURE CODE....4");}

            context.PixelShader.SetSampler(samplerState,0);
			
			if(debug){MessageBox.Show( "InitialiseGraphics()... TEXTURE CODE....7");}
			
			// 26/08/12: START
			// --------------------
			// INPUT ELEMENTS
			// ----------------------
			var elements = new[] { 
				new SlimDX.Direct3D11.InputElement("POSITION", 0, SlimDX.DXGI.Format.R32G32B32_Float, 0),
				new SlimDX.Direct3D11.InputElement("TEXCOORD", 0, SlimDX.DXGI.Format.R32G32_Float, 0)
			};
			
			if(debug){MessageBox.Show( "InitialiseGraphics()... TEXTURE CODE....8");}
            
			layout = new SlimDX.Direct3D11.InputLayout(graphics, inputSignature, elements);
			
			if(debug){MessageBox.Show( "InitialiseGraphics()... TEXTURE CODE....9");}
			// --------------------------------------------------------------------------------------------------------------------
			
			// -------------------------------------------
			// INPUT ASSEMBLER: Layout & PrimitiveTopology
			// -------------------------------------------
			context.InputAssembler.InputLayout = layout;
			
			if(debug){MessageBox.Show( "InitialiseGraphics()... TEXTURE CODE....10");}

			context.InputAssembler.PrimitiveTopology = SlimDX.Direct3D11.PrimitiveTopology.TriangleStrip;
			// -----------------------------------------------------------------------
			
			if(debug){MessageBox.Show( "InitialiseGraphics()... TEXTURE CODE....11");}
			
			// Added these because of the awful crashing incident. Might fix it?
			// set the shaders
            context.VertexShader.Set(vertexShader);
            context.PixelShader.Set(pixelShader);
			
			if(debug){MessageBox.Show( "InitialiseGraphics()... TEXTURE CODE....12");}
			
			// Clear the screen...
			context.ClearRenderTargetView(renderTarget, new SlimDX.Color4(0.0f, 0.0f, 0.0f));
			// 26/08/12: END
			
			if(debug){MessageBox.Show( "InitialiseGraphics()... TEXTURE CODE....13");}
		}
		// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		// END: Method InitialiseGraphics
		// >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

I'm slightly baffled, as I didn't think I'd hit this problem what with the power of hardware these days. Wierd. Clearly I'm doing something very silly.

 

blink.png

Share this post


Link to post
Share on other sites
Advertisement

You only intialize your graphics device once per run of the game (right????) so the problem isn't going to be in how you set things up, that's pretty standard, boilerplate DirectX stuff.  Your framerate drop could be coming from many sources:

 

Shader code

repetitive buffer creation

repetitive rendertarget creation

inefficient draw-call architecture (are you sending each tile as a sprite to the batcher, or telling the batcher to draw itself for every sprite?)

 

Or it could be entirely outside your graphics portion.  What happens in the update portion of your game loop?  The fact that an increase in your tile objects dropped your framerate makes me wonder if you have some insanely inefficient pathfinding algorithm (like recursively checking every tile/node against every other one), or collision tests that happen against every object in the scene, etc.

Edited by BCullis

Share this post


Link to post
Share on other sites

Yep - initialise graphics once only at the start. The shader and buffer handling is all done via the SpriteTextRenderer library I'm using, so I'm assuming it's not the problem at the moment else I'd be doing Nico a dis-service.

 

I tend to agree with your assertion that this could be a problem with another algorithm in the game. I better get hunting for it!

Share this post


Link to post
Share on other sites

Something you might try is wrapping your Draw() and Update() sections with some debug timer output: find out where your time is being spent per-frame.

Share this post


Link to post
Share on other sites
Rather than optimizing your entire code base over the course of 3 months and then finding out that it was some esoteric compiler option why not just run a profiler?

Share this post


Link to post
Share on other sites

Good point. I'm not that clued up to be honest, but the profiler has revealed that the problem might be the expense of texture switching!

Share this post


Link to post
Share on other sites

Right then, I've just tried an experiment which results in every tile being drawn with the same texture, and this has improved the frame rate remarkably... 225 from 5!

Share this post


Link to post
Share on other sites

You could try sorting your tiles by texture before drawing them and seeing how that effects performance.

 

It would be helpful if you could post some of your drawing code (i.e. the bits that are called every frame) to see if there are any obvious issues in there that could be causing problems.

Share this post


Link to post
Share on other sites

Thanks all.

 

I have written a routine to group draw calls by texture, and this helps a massive amount. The game is now zipping along. The profiler has been really useful, and I'm a fool for not using it before!

Share this post


Link to post
Share on other sites

Thanks all.
 
I have written a routine to group draw calls by texture, and this helps a massive amount. The game is now zipping along. The profiler has been really useful, and I'm a fool for not using it before!

I can only speak for myself, but I wouldn't be here myself, if I wasnt a fool. smile.png
You can turn it around too, and say that any game developers that doesn't use gamedev as an awesome source of information and knowledge, is the greater fool!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement