Skip to content

Commit

Permalink
atlas,d2d: overdraw background bitmap by one cell on all sides (#17674)
Browse files Browse the repository at this point in the history
BackendD2D will now draw one extra cell on all sides when rendering the
background, filled with the expected background color, starting at (-1,
-1) to ensure that cell backgrounds do not bleed over the edges of the
viewport where the is swapchain but no content.

Fixes #17672
  • Loading branch information
DHowett authored Aug 6, 2024
1 parent 07c7167 commit 9007fc2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
44 changes: 37 additions & 7 deletions src/renderer/atlas/BackendD2D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ void BackendD2D::_handleSettingsUpdate(const RenderingPayload& p)
const auto renderTargetChanged = !_renderTarget;
const auto fontChanged = _fontGeneration != p.s->font.generation();
const auto cursorChanged = _cursorGeneration != p.s->cursor.generation();
const auto backgroundColorChanged = _miscGeneration != p.s->misc.generation();
const auto cellCountChanged = _viewportCellCount != p.s->viewportCellCount;

if (renderTargetChanged)
Expand Down Expand Up @@ -125,26 +126,48 @@ void BackendD2D::_handleSettingsUpdate(const RenderingPayload& p)
_builtinGlyphsRenderTargetActive = false;
}

if (renderTargetChanged || fontChanged || cellCountChanged)
if (renderTargetChanged || fontChanged || cellCountChanged || backgroundColorChanged)
{
const D2D1_BITMAP_PROPERTIES props{
.pixelFormat = { DXGI_FORMAT_R8G8B8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED },
.dpiX = static_cast<f32>(p.s->font->dpi),
.dpiY = static_cast<f32>(p.s->font->dpi),
};
const D2D1_SIZE_U size{
p.s->viewportCellCount.x,
p.s->viewportCellCount.y,
p.s->viewportCellCount.x + 2u,
p.s->viewportCellCount.y + 2u,
};
const D2D1_MATRIX_3X2_F transform{
.m11 = static_cast<f32>(p.s->font->cellSize.x),
.m22 = static_cast<f32>(p.s->font->cellSize.y),
/* Brushes are transformed relative to the render target, not the rect into which they are painted. */
.dx = -static_cast<f32>(p.s->font->cellSize.x),
.dy = -static_cast<f32>(p.s->font->cellSize.y),
};
THROW_IF_FAILED(_renderTarget->CreateBitmap(size, nullptr, 0, &props, _backgroundBitmap.put()));

/*
We're allocating a bitmap that is one pixel wider on every side than the viewport so that we can fill in the gutter
with the background color. D2D doesn't have an equivalent to D3D11_TEXTURE_ADDRESS_BORDER, which we use in the D3D
backend to ensure the colors don't bleed off the edges.
XXXXXXXXXXXXXXXX <- background color
X+------------+X
X| viewport |X
X| |X
X| |X
X+------------+X
XXXXXXXXXXXXXXXX
The translation in `transform` ensures that we render it off the top left of the render target.
*/
auto backgroundFill = std::make_unique_for_overwrite<u32[]>(static_cast<size_t>(size.width) * size.height);
std::fill_n(backgroundFill.get(), size.width * size.height, u32ColorPremultiply(p.s->misc->backgroundColor));

THROW_IF_FAILED(_renderTarget->CreateBitmap(size, backgroundFill.get(), size.width * sizeof(u32), &props, _backgroundBitmap.put()));
THROW_IF_FAILED(_renderTarget->CreateBitmapBrush(_backgroundBitmap.get(), _backgroundBrush.put()));
_backgroundBrush->SetInterpolationMode(D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR);
_backgroundBrush->SetExtendModeX(D2D1_EXTEND_MODE_MIRROR);
_backgroundBrush->SetExtendModeY(D2D1_EXTEND_MODE_MIRROR);
_backgroundBrush->SetExtendModeX(D2D1_EXTEND_MODE_CLAMP);
_backgroundBrush->SetExtendModeY(D2D1_EXTEND_MODE_CLAMP);
_backgroundBrush->SetTransform(&transform);
_backgroundBitmapGeneration = {};
}
Expand All @@ -158,14 +181,21 @@ void BackendD2D::_handleSettingsUpdate(const RenderingPayload& p)
_generation = p.s.generation();
_fontGeneration = p.s->font.generation();
_cursorGeneration = p.s->cursor.generation();
_miscGeneration = p.s->misc.generation();
_viewportCellCount = p.s->viewportCellCount;
}

void BackendD2D::_drawBackground(const RenderingPayload& p)
{
if (_backgroundBitmapGeneration != p.colorBitmapGenerations[0])
{
THROW_IF_FAILED(_backgroundBitmap->CopyFromMemory(nullptr, p.backgroundBitmap.data(), gsl::narrow_cast<UINT32>(p.colorBitmapRowStride * sizeof(u32))));
const D2D1_RECT_U rect{
1u,
1u,
1u + p.s->viewportCellCount.x,
1u + p.s->viewportCellCount.y,
};
THROW_IF_FAILED(_backgroundBitmap->CopyFromMemory(&rect, p.backgroundBitmap.data(), gsl::narrow_cast<UINT32>(p.colorBitmapRowStride * sizeof(u32))));
_backgroundBitmapGeneration = p.colorBitmapGenerations[0];
}

Expand Down
1 change: 1 addition & 0 deletions src/renderer/atlas/BackendD2D.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ namespace Microsoft::Console::Render::Atlas
til::generation_t _generation;
til::generation_t _fontGeneration;
til::generation_t _cursorGeneration;
til::generation_t _miscGeneration;
u16x2 _viewportCellCount{};

#if ATLAS_DEBUG_SHOW_DIRTY
Expand Down

0 comments on commit 9007fc2

Please sign in to comment.