in ExampleGallery/Direct3DInterop/SpriteBatchPerformance.cpp [257:326]
virtual void RunScenario(CanvasDrawingSession^ drawingSession, CanvasSpriteSortMode sortMode)
{
UNREFERENCED_PARAMETER(sortMode); // sort mode is ignored
std::vector<ID2D1Bitmap*> bitmaps;
std::vector<D2D1_RECT_F> destRects;
std::vector<D2D1_COLOR_F> colors;
std::vector<D2D1_MATRIX_3X2_F> transforms;
for (auto const& sprite : m_sprites)
{
bitmaps.push_back(GetWrappedResource<ID2D1Bitmap>(sprite.Bitmap).Get());
auto size = sprite.Bitmap->Size;
destRects.push_back(D2D1_RECT_F{ 0, 0, size.Width, size.Height });
colors.push_back(D2D1_COLOR_F{ sprite.Tint.x, sprite.Tint.y, sprite.Tint.z, sprite.Tint.w });
transforms.push_back(D2D1::Matrix3x2F::Rotation(DirectX::XMConvertToDegrees(sprite.Rotation)) * D2D1::Matrix3x2F::Translation(sprite.Position.x, sprite.Position.y));
}
if (destRects.empty())
return;
auto deviceContext = GetWrappedResource<ID2D1DeviceContext3>(drawingSession);
auto oldAntialiasMode = deviceContext->GetAntialiasMode();
deviceContext->SetAntialiasMode(D2D1_ANTIALIAS_MODE_ALIASED);
ComPtr<ID2D1SpriteBatch> spriteBatch;
__abi_ThrowIfFailed(deviceContext->CreateSpriteBatch(&spriteBatch));
__abi_ThrowIfFailed(spriteBatch->AddSprites(
static_cast<uint32_t>(destRects.size()),
destRects.data(),
nullptr, // source rectangles
colors.data(),
transforms.data()));
uint32_t startIndex = 0;
uint32_t endIndex = 0;
ID2D1Bitmap* lastBitmap = bitmaps.front();
for (auto bitmap : bitmaps)
{
if (bitmap != lastBitmap)
{
deviceContext->DrawSpriteBatch(
spriteBatch.Get(),
startIndex,
endIndex - startIndex,
lastBitmap);
lastBitmap = bitmap;
startIndex = endIndex;
}
++endIndex;
}
if (endIndex > startIndex)
{
deviceContext->DrawSpriteBatch(
spriteBatch.Get(),
startIndex,
endIndex - startIndex,
lastBitmap);
}
deviceContext->SetAntialiasMode(oldAntialiasMode);
}