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.
![]()

Find content
Male
