This is a native Direct3D 11 implementation of the SpriteBatch helper from XNA Game Studio, providing identical functionality and API.
The SpriteBatch class requires a Direct3D 11 device context for drawing.
std::unique_ptr<SpriteBatch> spriteBatch(new SpriteBatch(deviceContext));
For exception safety, it is recommended you make use of the C++ RAII pattern and use a std::unique_ptr or std::shared_ptr
spriteBatch->Draw(texture, XMFLOAT2(x, y));
The Draw method has many overloads with parameters controlling:
- Specify position as XMFLOAT2, XMVECTOR or RECT
- Optional source rectangle for drawing just part of a sprite sheet
- Tint color
- Rotation (in radians)
- Origin point (position, scaling and rotation are relative to this)
- SpriteEffects enum (for horizontal or vertical mirroring)
- Layer depth (for sorting)
The first parameter to SpriteBatch::Begin is a SpriteSortMode enum. For most efficient rendering, use SpriteSortMode_Deferred (which batches up sprites, then submits them all to the GPU during the End call), and manually draw everything in texture order. If it is not possible to draw in texture order, the second most efficient approach is to use SpriteSortMode_Texture, which will automatically sort on your behalf.
When drawing scenes with multiple depth layers, SpriteSortMode_BackToFront or SpriteSortMode_FrontToBack will sort by the layerDepth parameter specified to each Draw call.
SpriteSortMode_Immediate disables all batching, submitting a separate Direct3D draw call for each sprite. This is expensive, but convenient in rare cases when you need to set shader constants differently per sprite.
Multiple SpriteBatch instances are lightweight. It is reasonable to create several, Begin them at the same time with different sort modes, submit sprites to different batches in arbitrary orders as you traverse a scene, then End the batches in whatever order you want these groups of sprites to be drawn.
Alpha blending defaults to using premultiplied alpha. To make use of 'straight' alpha textures, provide a blend state object to Begin.
spriteBatch->Begin(SpriteSortMode_Deferred, states.NonPremultiplied() );
Custom render states
By default SpriteBatch uses premultiplied alpha blending, no depth buffer, counter clockwise culling, and linear filtering with clamp texture addressing. You can change this by passing custom state objects to SpriteBatch::Begin. Pass null for any parameters that should use their default state.
To use SpriteBatch with a custom pixel shader (handy for 2D postprocessing effects such as bloom or blur) or even a custom vertex shader, use the setCustomShaders parameter to specify a state setting callback function:
spriteBatch->Begin(SpriteSortMode_Deferred, nullptr, nullptr, nullptr, nullptr, [=]
SpriteBatch automatically sets pixel shader resource #0 to the texture specified by each Draw call, so you only need to call PSSetResources for any additional textures required by your shader.
SpriteBatch::Begin also has a transformMatrix parameter, which can be used for global transforms such as scaling or translation of an entire scene.
Feature Level Notes
SpriteBatch's implementation supports all feature levels.
Creation is fully asynchronous, so you can instantiate multiple SpriteBatch instances at the same time on different threads. Each SpriteBatch instance only supports drawing from one thread at a time, but you can simultaneously submit sprites on multiple threads if you create a separate SpriteBatch instance per Direct3D 11 deferred context.http://msdn.microsoft.com/en-us/library/windows/desktop/ff476892.aspx