Batch text and line annotation rendering

This commit makes `LineRenderer` and `TextRenderer` to batch line and
text rendering. Batching them has shown a significant speedup,
especially when usin CUDA.
This commit is contained in:
Manish Mathai 2021-10-02 14:08:03 -07:00
parent 7b737ed2d5
commit 8f025a2c56
18 changed files with 677 additions and 253 deletions

@ -36,6 +36,7 @@ set(headers
DecodePNG.h # deprecated
EncodePNG.h # deprecated
LineRenderer.h
LineRendererBatcher.h
MatrixHelpers.h
Scene.h
Mapper.h
@ -52,6 +53,7 @@ set(headers
TextAnnotationBillboard.h
TextAnnotationScreen.h
TextRenderer.h
TextRendererBatcher.h
Texture2D.h
Triangulator.h
View.h
@ -85,6 +87,7 @@ set(device_sources
ColorLegendAnnotation.cxx
ConnectivityProxy.cxx
LineRenderer.cxx
LineRendererBatcher.cxx
Mapper.cxx
MapperConnectivity.cxx
MapperCylinder.cxx
@ -99,6 +102,7 @@ set(device_sources
TextAnnotationBillboard.cxx
TextAnnotationScreen.cxx
TextRenderer.cxx
TextRendererBatcher.cxx
View.cxx
View1D.cxx
View2D.cxx

@ -20,6 +20,7 @@
#include <vtkm/rendering/BitmapFontFactory.h>
#include <vtkm/rendering/LineRenderer.h>
#include <vtkm/rendering/TextRenderer.h>
#include <vtkm/rendering/TextRendererBatcher.h>
#include <vtkm/rendering/WorldAnnotator.h>
#include <vtkm/worklet/DispatcherMapField.h>
#include <vtkm/worklet/WorkletMapField.h>
@ -224,6 +225,7 @@ struct Canvas::CanvasInternals
FontTextureType FontTexture;
vtkm::Matrix<vtkm::Float32, 4, 4> ModelView;
vtkm::Matrix<vtkm::Float32, 4, 4> Projection;
std::shared_ptr<vtkm::rendering::TextRendererBatcher> TextBatcher;
};
Canvas::Canvas(vtkm::Id width, vtkm::Id height)
@ -394,8 +396,11 @@ void Canvas::AddLine(const vtkm::Vec2f_64& point0,
const vtkm::rendering::Color& color) const
{
vtkm::rendering::Canvas* self = const_cast<vtkm::rendering::Canvas*>(this);
LineRenderer renderer(self, vtkm::MatrixMultiply(Internals->Projection, Internals->ModelView));
vtkm::rendering::LineRendererBatcher lineBatcher;
LineRenderer renderer(
self, vtkm::MatrixMultiply(Internals->Projection, Internals->ModelView), &lineBatcher);
renderer.RenderLine(point0, point1, linewidth, color);
lineBatcher.Render(self);
}
void Canvas::AddLine(vtkm::Float64 x0,
@ -476,16 +481,14 @@ void Canvas::AddText(const vtkm::Matrix<vtkm::Float32, 4, 4>& transform,
const std::string& text,
const vtkm::Float32& depth) const
{
if (!Internals->FontTexture.IsValid())
if (!this->EnsureFontLoaded() || !this->Internals->TextBatcher)
{
if (!LoadFont())
{
return;
}
return;
}
vtkm::rendering::Canvas* self = const_cast<vtkm::rendering::Canvas*>(this);
TextRenderer fontRenderer(self, Internals->Font, Internals->FontTexture);
TextRenderer fontRenderer(
self, Internals->Font, Internals->FontTexture, this->Internals->TextBatcher.get());
fontRenderer.RenderText(transform, scale, anchor, color, text, depth);
}
@ -527,6 +530,40 @@ void Canvas::AddText(vtkm::Float32 x,
text);
}
void Canvas::BeginTextRenderingBatch() const
{
if (!this->EnsureFontLoaded() || this->Internals->TextBatcher)
{
return;
}
this->Internals->TextBatcher =
std::make_shared<vtkm::rendering::TextRendererBatcher>(this->Internals->FontTexture);
}
void Canvas::EndTextRenderingBatch() const
{
if (!this->Internals->TextBatcher)
{
return;
}
this->Internals->TextBatcher->Render(this);
this->Internals->TextBatcher.reset();
}
bool Canvas::EnsureFontLoaded() const
{
if (!Internals->FontTexture.IsValid())
{
if (!LoadFont())
{
return false;
}
}
return true;
}
bool Canvas::LoadFont() const
{
Internals->Font = BitmapFontFactory::CreateLiberation2Sans();

@ -204,6 +204,11 @@ public:
const std::string& text,
const vtkm::Float32& depth = 0) const;
VTKM_CONT
void BeginTextRenderingBatch() const;
VTKM_CONT
void EndTextRenderingBatch() const;
friend class AxisAnnotation2D;
friend class ColorBarAnnotation;
@ -215,6 +220,8 @@ public:
private:
bool LoadFont() const;
bool EnsureFontLoaded() const;
const vtkm::Matrix<vtkm::Float32, 4, 4>& GetModelView() const;
const vtkm::Matrix<vtkm::Float32, 4, 4>& GetProjection() const;

@ -12,6 +12,7 @@
#include <vtkm/Transform3D.h>
#include <vtkm/cont/TryExecute.h>
#include <vtkm/rendering/LineRendererBatcher.h>
#include <vtkm/worklet/DispatcherMapField.h>
#include <vtkm/worklet/WorkletMapField.h>
@ -21,9 +22,11 @@ namespace rendering
{
LineRenderer::LineRenderer(const vtkm::rendering::Canvas* canvas,
vtkm::Matrix<vtkm::Float32, 4, 4> transform)
vtkm::Matrix<vtkm::Float32, 4, 4> transform,
vtkm::rendering::LineRendererBatcher* lineBatcher)
: Canvas(canvas)
, Transform(transform)
, LineBatcher(lineBatcher)
{
}
@ -45,85 +48,7 @@ void LineRenderer::RenderLine(const vtkm::Vec3f_64& point0,
{
vtkm::Vec3f_32 p0 = TransformPoint(point0);
vtkm::Vec3f_32 p1 = TransformPoint(point1);
vtkm::Id x0 = static_cast<vtkm::Id>(vtkm::Round(p0[0]));
vtkm::Id y0 = static_cast<vtkm::Id>(vtkm::Round(p0[1]));
vtkm::Float32 z0 = static_cast<vtkm::Float32>(p0[2]);
vtkm::Id x1 = static_cast<vtkm::Id>(vtkm::Round(p1[0]));
vtkm::Id y1 = static_cast<vtkm::Id>(vtkm::Round(p1[1]));
vtkm::Float32 z1 = static_cast<vtkm::Float32>(p1[2]);
vtkm::Id dx = vtkm::Abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
vtkm::Id dy = -vtkm::Abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
vtkm::Id err = dx + dy, err2 = 0;
const vtkm::Id xStart = x0;
const vtkm::Id yStart = y0;
const vtkm::Float32 pdist = vtkm::Sqrt(vtkm::Float32(dx * dx) + vtkm::Float32(dy * dy));
auto colorPortal =
vtkm::rendering::Canvas::ColorBufferType(Canvas->GetColorBuffer()).WritePortal();
auto depthPortal =
vtkm::rendering::Canvas::DepthBufferType(Canvas->GetDepthBuffer()).WritePortal();
vtkm::Vec4f_32 colorC = color.Components;
while (x0 >= 0 && x0 < Canvas->GetWidth() && y0 >= 0 && y0 < Canvas->GetHeight())
{
vtkm::Float32 deltaX = static_cast<vtkm::Float32>(x0 - xStart);
vtkm::Float32 deltaY = static_cast<vtkm::Float32>(y0 - yStart);
// Depth is wrong, but its far less wrong that it used to be.
// These depth values are in screen space, which have been
// potentially tranformed by a perspective correction.
// To interpolated the depth correctly, there must be a perspective correction.
// I haven't looked, but the wireframmer probably suffers from this too.
// Additionally, this should not happen on the CPU. Annotations take
// far longer than the the geometry.
vtkm::Float32 t = pdist == 0.f ? 1.0f : vtkm::Sqrt(deltaX * deltaX + deltaY * deltaY) / pdist;
t = vtkm::Min(1.f, vtkm::Max(0.f, t));
vtkm::Float32 z = vtkm::Lerp(z0, z1, t);
vtkm::Id index = y0 * Canvas->GetWidth() + x0;
vtkm::Vec4f_32 currentColor = colorPortal.Get(index);
vtkm::Float32 currentZ = depthPortal.Get(index);
bool blend = currentColor[3] < 1.f && z > currentZ;
if (currentZ > z || blend)
{
vtkm::Vec4f_32 writeColor = colorC;
vtkm::Float32 depth = z;
if (blend)
{
// If there is any transparency, all alphas
// have been pre-mulitplied
vtkm::Float32 alpha = (1.f - currentColor[3]);
writeColor[0] = currentColor[0] + colorC[0] * alpha;
writeColor[1] = currentColor[1] + colorC[1] * alpha;
writeColor[2] = currentColor[2] + colorC[2] * alpha;
writeColor[3] = 1.f * alpha + currentColor[3]; // we are always drawing opaque lines
// keep the current z. Line z interpolation is not accurate
// Matt: this is correct. Interpolation is wrong
depth = currentZ;
}
depthPortal.Set(index, depth);
colorPortal.Set(index, writeColor);
}
if (x0 == x1 && y0 == y1)
{
break;
}
err2 = err * 2;
if (err2 >= dy)
{
err += dy;
x0 += sx;
}
if (err2 <= dx)
{
err += dx;
y0 += sy;
}
}
this->LineBatcher->BatchLine(p0, p1, color);
}
vtkm::Vec3f_32 LineRenderer::TransformPoint(const vtkm::Vec3f_64& point) const

@ -20,12 +20,15 @@ namespace vtkm
{
namespace rendering
{
class LineRendererBatcher;
class VTKM_RENDERING_EXPORT LineRenderer
{
public:
VTKM_CONT
LineRenderer(const vtkm::rendering::Canvas* canvas, vtkm::Matrix<vtkm::Float32, 4, 4> transform);
LineRenderer(const vtkm::rendering::Canvas* canvas,
vtkm::Matrix<vtkm::Float32, 4, 4> transform,
vtkm::rendering::LineRendererBatcher* lineBatcher);
VTKM_CONT
void RenderLine(const vtkm::Vec2f_64& point0,
@ -45,6 +48,7 @@ private:
const vtkm::rendering::Canvas* Canvas;
vtkm::Matrix<vtkm::Float32, 4, 4> Transform;
vtkm::rendering::LineRendererBatcher* LineBatcher;
}; // class LineRenderer
}
} // namespace vtkm::rendering

@ -0,0 +1,210 @@
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
//
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//============================================================================
#include <vtkm/rendering/LineRendererBatcher.h>
#include <vtkm/worklet/DispatcherMapField.h>
#include <vtkm/worklet/WorkletMapField.h>
namespace vtkm
{
namespace rendering
{
namespace
{
using ColorsArrayHandle = vtkm::cont::ArrayHandle<vtkm::Vec4f_32>;
using PointsArrayHandle = vtkm::cont::ArrayHandle<vtkm::Vec3f_32>;
struct RenderLine : public vtkm::worklet::WorkletMapField
{
using ColorBufferType = vtkm::rendering::Canvas::ColorBufferType;
using DepthBufferType = vtkm::rendering::Canvas::DepthBufferType;
using ControlSignature = void(FieldIn, FieldIn, FieldIn, WholeArrayInOut, WholeArrayInOut);
using ExecutionSignature = void(_1, _2, _3, _4, _5);
using InputDomain = _1;
VTKM_CONT
RenderLine() {}
VTKM_CONT
RenderLine(vtkm::Id width, vtkm::Id height)
: Width(width)
, Height(height)
{
}
template <typename ColorBufferPortal, typename DepthBufferPortal>
VTKM_EXEC void operator()(const vtkm::Vec3f_32& start,
const vtkm::Vec3f_32& end,
const vtkm::Vec4f_32& color,
ColorBufferPortal& colorBuffer,
DepthBufferPortal& depthBuffer) const
{
vtkm::Id x0 = static_cast<vtkm::Id>(vtkm::Round(start[0]));
vtkm::Id y0 = static_cast<vtkm::Id>(vtkm::Round(start[1]));
vtkm::Float32 z0 = static_cast<vtkm::Float32>(start[2]);
vtkm::Id x1 = static_cast<vtkm::Id>(vtkm::Round(end[0]));
vtkm::Id y1 = static_cast<vtkm::Id>(vtkm::Round(end[1]));
vtkm::Float32 z1 = static_cast<vtkm::Float32>(end[2]);
vtkm::Id dx = vtkm::Abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
vtkm::Id dy = -vtkm::Abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
vtkm::Id err = dx + dy, err2 = 0;
const vtkm::Id xStart = x0;
const vtkm::Id yStart = y0;
const vtkm::Float32 pdist = vtkm::Sqrt(vtkm::Float32(dx * dx) + vtkm::Float32(dy * dy));
while (x0 >= 0 && x0 < this->Width && y0 >= 0 && y0 < this->Height)
{
vtkm::Float32 deltaX = static_cast<vtkm::Float32>(x0 - xStart);
vtkm::Float32 deltaY = static_cast<vtkm::Float32>(y0 - yStart);
// Depth is wrong, but its far less wrong that it used to be.
// These depth values are in screen space, which have been
// potentially tranformed by a perspective correction.
// To interpolated the depth correctly, there must be a perspective correction.
// I haven't looked, but the wireframmer probably suffers from this too.
// Additionally, this should not happen on the CPU. Annotations take
// far longer than the the geometry.
vtkm::Float32 t = pdist == 0.f ? 1.0f : vtkm::Sqrt(deltaX * deltaX + deltaY * deltaY) / pdist;
t = vtkm::Min(1.f, vtkm::Max(0.f, t));
vtkm::Float32 z = vtkm::Lerp(z0, z1, t);
vtkm::Id index = y0 * this->Width + x0;
vtkm::Vec4f_32 currentColor = colorBuffer.Get(index);
vtkm::Float32 currentZ = depthBuffer.Get(index);
bool blend = currentColor[3] < 1.f && z > currentZ;
if (currentZ > z || blend)
{
vtkm::Vec4f_32 writeColor = color;
vtkm::Float32 depth = z;
if (blend)
{
// If there is any transparency, all alphas
// have been pre-mulitplied
vtkm::Float32 alpha = (1.f - currentColor[3]);
writeColor[0] = currentColor[0] + color[0] * alpha;
writeColor[1] = currentColor[1] + color[1] * alpha;
writeColor[2] = currentColor[2] + color[2] * alpha;
writeColor[3] = 1.f * alpha + currentColor[3]; // we are always drawing opaque lines
// keep the current z. Line z interpolation is not accurate
// Matt: this is correct. Interpolation is wrong
depth = currentZ;
}
depthBuffer.Set(index, depth);
colorBuffer.Set(index, writeColor);
}
if (x0 == x1 && y0 == y1)
{
break;
}
err2 = err * 2;
if (err2 >= dy)
{
err += dy;
x0 += sx;
}
if (err2 <= dx)
{
err += dx;
y0 += sy;
}
}
}
vtkm::Id Width;
vtkm::Id Height;
}; // struct RenderLine
struct RenderLineExecutor
{
using ColorBufferType = vtkm::rendering::Canvas::ColorBufferType;
using DepthBufferType = vtkm::rendering::Canvas::DepthBufferType;
VTKM_CONT
RenderLineExecutor(const PointsArrayHandle& starts,
const PointsArrayHandle& ends,
const ColorsArrayHandle& colors,
vtkm::Id width,
vtkm::Id height,
const ColorBufferType& colorBuffer,
const DepthBufferType& depthBuffer)
: Starts(starts)
, Ends(ends)
, Colors(colors)
, Worklet(width, height)
, ColorBuffer(colorBuffer)
, DepthBuffer(depthBuffer)
{
}
template <typename Device>
VTKM_CONT bool operator()(Device) const
{
VTKM_IS_DEVICE_ADAPTER_TAG(Device);
vtkm::worklet::DispatcherMapField<RenderLine> dispatcher(Worklet);
dispatcher.SetDevice(Device());
dispatcher.Invoke(Starts, Ends, Colors, ColorBuffer, DepthBuffer);
return true;
}
PointsArrayHandle Starts;
PointsArrayHandle Ends;
ColorsArrayHandle Colors;
RenderLine Worklet;
ColorBufferType ColorBuffer;
DepthBufferType DepthBuffer;
}; // struct RenderLineExecutor
} // namespace
LineRendererBatcher::LineRendererBatcher() {}
void LineRendererBatcher::BatchLine(const vtkm::Vec3f_64& start,
const vtkm::Vec3f_64& end,
const vtkm::rendering::Color& color)
{
vtkm::Vec3f_32 start32(static_cast<vtkm::Float32>(start[0]),
static_cast<vtkm::Float32>(start[1]),
static_cast<vtkm::Float32>(start[2]));
vtkm::Vec3f_32 end32(static_cast<vtkm::Float32>(end[0]),
static_cast<vtkm::Float32>(end[1]),
static_cast<vtkm::Float32>(end[2]));
this->BatchLine(start32, end32, color);
}
void LineRendererBatcher::BatchLine(const vtkm::Vec3f_32& start,
const vtkm::Vec3f_32& end,
const vtkm::rendering::Color& color)
{
this->Starts.push_back(start);
this->Ends.push_back(end);
this->Colors.push_back(color.Components);
}
void LineRendererBatcher::Render(const vtkm::rendering::Canvas* canvas) const
{
PointsArrayHandle starts = vtkm::cont::make_ArrayHandle(this->Starts);
PointsArrayHandle ends = vtkm::cont::make_ArrayHandle(this->Ends);
ColorsArrayHandle colors = vtkm::cont::make_ArrayHandle(this->Colors);
vtkm::cont::TryExecute(RenderLineExecutor(starts,
ends,
colors,
canvas->GetWidth(),
canvas->GetHeight(),
canvas->GetColorBuffer(),
canvas->GetDepthBuffer()));
}
}
} // namespace vtkm::rendering

@ -0,0 +1,52 @@
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
//
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//============================================================================
#ifndef vtk_m_rendering_LineRendererBatcher_h
#define vtk_m_rendering_LineRendererBatcher_h
#include <string>
#include <vector>
#include <vtkm/rendering/Canvas.h>
#include <vtkm/rendering/Color.h>
#include <vtkm/rendering/vtkm_rendering_export.h>
namespace vtkm
{
namespace rendering
{
class VTKM_RENDERING_EXPORT LineRendererBatcher
{
public:
VTKM_CONT
LineRendererBatcher();
VTKM_CONT
void BatchLine(const vtkm::Vec3f_64& start,
const vtkm::Vec3f_64& end,
const vtkm::rendering::Color& color);
VTKM_CONT
void BatchLine(const vtkm::Vec3f_32& start,
const vtkm::Vec3f_32& end,
const vtkm::rendering::Color& color);
void Render(const vtkm::rendering::Canvas* canvas) const;
private:
std::vector<vtkm::Vec3f_32> Starts;
std::vector<vtkm::Vec3f_32> Ends;
std::vector<vtkm::Vec4f_32> Colors;
};
}
} // namespace vtkm::rendering
#endif // vtk_m_rendering_LineRendererBatcher_h

@ -12,6 +12,7 @@
#include <vtkm/Transform3D.h>
#include <vtkm/cont/TryExecute.h>
#include <vtkm/rendering/TextRendererBatcher.h>
#include <vtkm/worklet/DispatcherMapField.h>
#include <vtkm/worklet/WorkletMapField.h>
@ -19,163 +20,15 @@ namespace vtkm
{
namespace rendering
{
namespace internal
{
struct RenderBitmapFont : public vtkm::worklet::WorkletMapField
{
using ColorBufferType = vtkm::rendering::Canvas::ColorBufferType;
using DepthBufferType = vtkm::rendering::Canvas::DepthBufferType;
using FontTextureType = vtkm::rendering::Canvas::FontTextureType;
using ControlSignature = void(FieldIn, FieldIn, ExecObject, WholeArrayInOut, WholeArrayInOut);
using ExecutionSignature = void(_1, _2, _3, _4, _5);
using InputDomain = _1;
VTKM_CONT
RenderBitmapFont() {}
VTKM_CONT
RenderBitmapFont(const vtkm::Vec4f_32& color,
vtkm::Id width,
vtkm::Id height,
vtkm::Float32 depth)
: Color(color)
, Width(width)
, Height(height)
, Depth(depth)
{
}
template <typename ColorBufferPortal, typename FontTexture, typename DepthBufferPortal>
VTKM_EXEC void operator()(const vtkm::Vec4f_32& screenCoords,
const vtkm::Vec4f_32& textureCoords,
const FontTexture& fontTexture,
ColorBufferPortal& colorBuffer,
DepthBufferPortal& depthBuffer) const
{
vtkm::Float32 x0 = Clamp(screenCoords[0], 0.0f, static_cast<vtkm::Float32>(Width - 1));
vtkm::Float32 x1 = Clamp(screenCoords[2], 0.0f, static_cast<vtkm::Float32>(Width - 1));
vtkm::Float32 y0 = Clamp(screenCoords[1], 0.0f, static_cast<vtkm::Float32>(Height - 1));
vtkm::Float32 y1 = Clamp(screenCoords[3], 0.0f, static_cast<vtkm::Float32>(Height - 1));
// For crisp text rendering, we sample the font texture at points smaller than the pixel
// sizes. Here we sample at increments of 0.25f, and scale the reported intensities accordingly
vtkm::Float32 dx = x1 - x0, dy = y1 - y0;
for (vtkm::Float32 x = x0; x <= x1; x += 0.25f)
{
for (vtkm::Float32 y = y0; y <= y1; y += 0.25f)
{
vtkm::Float32 tu = x1 == x0 ? 1.0f : (x - x0) / dx;
vtkm::Float32 tv = y1 == y0 ? 1.0f : (y - y0) / dy;
vtkm::Float32 u = vtkm::Lerp(textureCoords[0], textureCoords[2], tu);
vtkm::Float32 v = vtkm::Lerp(textureCoords[1], textureCoords[3], tv);
vtkm::Float32 intensity = fontTexture.GetColor(u, v)[0] * 0.25f;
Plot(x, y, intensity, colorBuffer, depthBuffer);
}
}
}
template <typename ColorBufferPortal, typename DepthBufferPortal>
VTKM_EXEC void Plot(vtkm::Float32 x,
vtkm::Float32 y,
vtkm::Float32 intensity,
ColorBufferPortal& colorBuffer,
DepthBufferPortal& depthBuffer) const
{
vtkm::Id index =
static_cast<vtkm::Id>(vtkm::Round(y)) * Width + static_cast<vtkm::Id>(vtkm::Round(x));
vtkm::Vec4f_32 srcColor = colorBuffer.Get(index);
vtkm::Float32 currentDepth = depthBuffer.Get(index);
bool swap = Depth > currentDepth;
intensity = intensity * Color[3];
vtkm::Vec4f_32 color = intensity * Color;
color[3] = intensity;
vtkm::Vec4f_32 front = color;
vtkm::Vec4f_32 back = srcColor;
if (swap)
{
front = srcColor;
back = color;
}
vtkm::Vec4f_32 blendedColor;
vtkm::Float32 alpha = (1.f - front[3]);
blendedColor[0] = front[0] + back[0] * alpha;
blendedColor[1] = front[1] + back[1] * alpha;
blendedColor[2] = front[2] + back[2] * alpha;
blendedColor[3] = back[3] * alpha + front[3];
colorBuffer.Set(index, blendedColor);
}
VTKM_EXEC
vtkm::Float32 Clamp(vtkm::Float32 v, vtkm::Float32 min, vtkm::Float32 max) const
{
return vtkm::Min(vtkm::Max(v, min), max);
}
vtkm::Vec4f_32 Color;
vtkm::Id Width;
vtkm::Id Height;
vtkm::Float32 Depth;
}; // struct RenderBitmapFont
struct RenderBitmapFontExecutor
{
using ColorBufferType = vtkm::rendering::Canvas::ColorBufferType;
using DepthBufferType = vtkm::rendering::Canvas::DepthBufferType;
using FontTextureType = vtkm::rendering::Canvas::FontTextureType;
using ScreenCoordsArrayHandle = vtkm::cont::ArrayHandle<vtkm::Id4>;
using TextureCoordsArrayHandle = vtkm::cont::ArrayHandle<vtkm::Vec4f_32>;
VTKM_CONT
RenderBitmapFontExecutor(const ScreenCoordsArrayHandle& screenCoords,
const TextureCoordsArrayHandle& textureCoords,
const FontTextureType& fontTexture,
const vtkm::Vec4f_32& color,
const ColorBufferType& colorBuffer,
const DepthBufferType& depthBuffer,
vtkm::Id width,
vtkm::Id height,
vtkm::Float32 depth)
: ScreenCoords(screenCoords)
, TextureCoords(textureCoords)
, FontTexture(fontTexture)
, ColorBuffer(colorBuffer)
, DepthBuffer(depthBuffer)
, Worklet(color, width, height, depth)
{
}
template <typename Device>
VTKM_CONT bool operator()(Device) const
{
VTKM_IS_DEVICE_ADAPTER_TAG(Device);
vtkm::worklet::DispatcherMapField<RenderBitmapFont> dispatcher(Worklet);
dispatcher.SetDevice(Device());
dispatcher.Invoke(
ScreenCoords, TextureCoords, FontTexture.GetExecObjectFactory(), ColorBuffer, DepthBuffer);
return true;
}
ScreenCoordsArrayHandle ScreenCoords;
TextureCoordsArrayHandle TextureCoords;
FontTextureType FontTexture;
ColorBufferType ColorBuffer;
DepthBufferType DepthBuffer;
RenderBitmapFont Worklet;
}; // struct RenderBitmapFontExecutor
} // namespace internal
TextRenderer::TextRenderer(const vtkm::rendering::Canvas* canvas,
const vtkm::rendering::BitmapFont& font,
const vtkm::rendering::Canvas::FontTextureType& fontTexture)
const vtkm::rendering::Canvas::FontTextureType& fontTexture,
vtkm::rendering::TextRendererBatcher* textBatcher)
: Canvas(canvas)
, Font(font)
, FontTexture(fontTexture)
, TextBatcher(textBatcher)
{
}
@ -226,8 +79,8 @@ void TextRenderer::RenderText(const vtkm::Matrix<vtkm::Float32, 4, 4>& transform
vtkm::Float32 fy = -(0.5f + 0.5f * anchor[1]);
vtkm::Float32 fz = 0;
using ScreenCoordsArrayHandle = internal::RenderBitmapFontExecutor::ScreenCoordsArrayHandle;
using TextureCoordsArrayHandle = internal::RenderBitmapFontExecutor::TextureCoordsArrayHandle;
using ScreenCoordsArrayHandle = vtkm::cont::ArrayHandle<vtkm::Id4>;
using TextureCoordsArrayHandle = vtkm::cont::ArrayHandle<vtkm::Vec4f_32>;
ScreenCoordsArrayHandle screenCoords;
TextureCoordsArrayHandle textureCoords;
{
@ -261,15 +114,7 @@ void TextRenderer::RenderText(const vtkm::Matrix<vtkm::Float32, 4, 4>& transform
}
}
vtkm::cont::TryExecute(internal::RenderBitmapFontExecutor(screenCoords,
textureCoords,
FontTexture,
color.Components,
Canvas->GetColorBuffer(),
Canvas->GetDepthBuffer(),
Canvas->GetWidth(),
Canvas->GetHeight(),
depth));
this->TextBatcher->BatchText(screenCoords, textureCoords, color, depth);
}
}
} // namespace vtkm::rendering

@ -22,6 +22,7 @@ namespace vtkm
{
namespace rendering
{
class TextRendererBatcher;
class VTKM_RENDERING_EXPORT TextRenderer
{
@ -29,7 +30,8 @@ public:
VTKM_CONT
TextRenderer(const vtkm::rendering::Canvas* canvas,
const vtkm::rendering::BitmapFont& font,
const vtkm::rendering::Canvas::FontTextureType& fontTexture);
const vtkm::rendering::Canvas::FontTextureType& fontTexture,
vtkm::rendering::TextRendererBatcher* textBatcher);
VTKM_CONT
void RenderText(const vtkm::Vec2f_32& position,
@ -61,6 +63,7 @@ private:
const vtkm::rendering::Canvas* Canvas;
vtkm::rendering::BitmapFont Font;
vtkm::rendering::Canvas::FontTextureType FontTexture;
vtkm::rendering::TextRendererBatcher* TextBatcher;
};
}
} // namespace vtkm::rendering

@ -0,0 +1,229 @@
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
//
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//============================================================================
#include <vtkm/rendering/TextRendererBatcher.h>
#include <vtkm/Transform3D.h>
#include <vtkm/cont/Timer.h>
#include <vtkm/cont/TryExecute.h>
#include <vtkm/worklet/DispatcherMapField.h>
#include <vtkm/worklet/WorkletMapField.h>
// TODO: mmathai - Remove this
#include <vtkm/AnnDebug.h>
namespace vtkm
{
namespace rendering
{
namespace
{
struct RenderBitmapFont : public vtkm::worklet::WorkletMapField
{
using ColorBufferType = vtkm::rendering::Canvas::ColorBufferType;
using DepthBufferType = vtkm::rendering::Canvas::DepthBufferType;
using FontTextureType = vtkm::rendering::Canvas::FontTextureType;
using ControlSignature =
void(FieldIn, FieldIn, FieldIn, FieldIn, ExecObject, WholeArrayInOut, WholeArrayInOut);
using ExecutionSignature = void(_1, _2, _3, _4, _5, _6, _7);
using InputDomain = _1;
VTKM_CONT
RenderBitmapFont() {}
VTKM_CONT
RenderBitmapFont(vtkm::Id width, vtkm::Id height)
: Width(width)
, Height(height)
{
}
template <typename ColorBufferPortal, typename FontTexture, typename DepthBufferPortal>
VTKM_EXEC void operator()(const vtkm::Vec4f_32& screenCoords,
const vtkm::Vec4f_32& textureCoords,
const vtkm::Vec4f_32& color,
const vtkm::Float32& depth,
const FontTexture& fontTexture,
ColorBufferPortal& colorBuffer,
DepthBufferPortal& depthBuffer) const
{
vtkm::Float32 x0 = Clamp(screenCoords[0], 0.0f, static_cast<vtkm::Float32>(Width - 1));
vtkm::Float32 x1 = Clamp(screenCoords[2], 0.0f, static_cast<vtkm::Float32>(Width - 1));
vtkm::Float32 y0 = Clamp(screenCoords[1], 0.0f, static_cast<vtkm::Float32>(Height - 1));
vtkm::Float32 y1 = Clamp(screenCoords[3], 0.0f, static_cast<vtkm::Float32>(Height - 1));
// For crisp text rendering, we sample the font texture at points smaller than the pixel
// sizes. Here we sample at increments of 0.25f, and scale the reported intensities accordingly
vtkm::Float32 dx = x1 - x0, dy = y1 - y0;
for (vtkm::Float32 x = x0; x <= x1; x += 0.25f)
{
for (vtkm::Float32 y = y0; y <= y1; y += 0.25f)
{
vtkm::Float32 tu = x1 == x0 ? 1.0f : (x - x0) / dx;
vtkm::Float32 tv = y1 == y0 ? 1.0f : (y - y0) / dy;
vtkm::Float32 u = vtkm::Lerp(textureCoords[0], textureCoords[2], tu);
vtkm::Float32 v = vtkm::Lerp(textureCoords[1], textureCoords[3], tv);
vtkm::Float32 intensity = fontTexture.GetColor(u, v)[0] * 0.25f;
Plot(x, y, intensity, color, depth, colorBuffer, depthBuffer);
}
}
}
template <typename ColorBufferPortal, typename DepthBufferPortal>
VTKM_EXEC void Plot(vtkm::Float32 x,
vtkm::Float32 y,
vtkm::Float32 intensity,
vtkm::Vec4f_32 color,
vtkm::Float32 depth,
ColorBufferPortal& colorBuffer,
DepthBufferPortal& depthBuffer) const
{
vtkm::Id index =
static_cast<vtkm::Id>(vtkm::Round(y)) * Width + static_cast<vtkm::Id>(vtkm::Round(x));
vtkm::Vec4f_32 srcColor = colorBuffer.Get(index);
vtkm::Float32 currentDepth = depthBuffer.Get(index);
bool swap = depth > currentDepth;
intensity = intensity * color[3];
color = intensity * color;
color[3] = intensity;
vtkm::Vec4f_32 front = color;
vtkm::Vec4f_32 back = srcColor;
if (swap)
{
front = srcColor;
back = color;
}
vtkm::Vec4f_32 blendedColor;
vtkm::Float32 alpha = (1.f - front[3]);
blendedColor[0] = front[0] + back[0] * alpha;
blendedColor[1] = front[1] + back[1] * alpha;
blendedColor[2] = front[2] + back[2] * alpha;
blendedColor[3] = back[3] * alpha + front[3];
colorBuffer.Set(index, blendedColor);
}
VTKM_EXEC
vtkm::Float32 Clamp(vtkm::Float32 v, vtkm::Float32 min, vtkm::Float32 max) const
{
return vtkm::Min(vtkm::Max(v, min), max);
}
vtkm::Id Width;
vtkm::Id Height;
}; // struct RenderBitmapFont
struct RenderBitmapFontExecutor
{
using ColorBufferType = vtkm::rendering::Canvas::ColorBufferType;
using DepthBufferType = vtkm::rendering::Canvas::DepthBufferType;
using FontTextureType = vtkm::rendering::TextRendererBatcher::FontTextureType;
using ScreenCoordsArrayHandle = vtkm::rendering::TextRendererBatcher::ScreenCoordsArrayHandle;
using TextureCoordsArrayHandle = vtkm::rendering::TextRendererBatcher::TextureCoordsArrayHandle;
using ColorsArrayHandle = vtkm::rendering::TextRendererBatcher::ColorsArrayHandle;
using DepthsArrayHandle = vtkm::rendering::TextRendererBatcher::DepthsArrayHandle;
VTKM_CONT
RenderBitmapFontExecutor(const ScreenCoordsArrayHandle& screenCoords,
const TextureCoordsArrayHandle& textureCoords,
const FontTextureType& fontTexture,
const ColorsArrayHandle& colors,
const ColorBufferType& colorBuffer,
const DepthBufferType& depthBuffer,
vtkm::Id width,
vtkm::Id height,
const DepthsArrayHandle& depths)
: ScreenCoords(screenCoords)
, TextureCoords(textureCoords)
, FontTexture(fontTexture)
, Colors(colors)
, ColorBuffer(colorBuffer)
, DepthBuffer(depthBuffer)
, Worklet(width, height)
, Depths(depths)
{
}
template <typename Device>
VTKM_CONT bool operator()(Device) const
{
VTKM_IS_DEVICE_ADAPTER_TAG(Device);
vtkm::worklet::DispatcherMapField<RenderBitmapFont> dispatcher(Worklet);
dispatcher.SetDevice(Device());
dispatcher.Invoke(ScreenCoords,
TextureCoords,
Colors,
Depths,
FontTexture.GetExecObjectFactory(),
ColorBuffer,
DepthBuffer);
return true;
}
ScreenCoordsArrayHandle ScreenCoords;
TextureCoordsArrayHandle TextureCoords;
FontTextureType FontTexture;
ColorsArrayHandle Colors;
ColorBufferType ColorBuffer;
DepthBufferType DepthBuffer;
RenderBitmapFont Worklet;
DepthsArrayHandle Depths;
}; // struct RenderBitmapFontExecutor
} // namespace
// TextRendererBatcher::TextRendererBatcher() {}
TextRendererBatcher::TextRendererBatcher(
const vtkm::rendering::Canvas::FontTextureType& fontTexture)
: FontTexture(fontTexture)
{
}
void TextRendererBatcher::BatchText(const ScreenCoordsArrayHandle& screenCoords,
const TextureCoordsArrayHandle& textureCoords,
const vtkm::rendering::Color& color,
const vtkm::Float32& depth)
{
vtkm::Id textLength = screenCoords.GetNumberOfValues();
ScreenCoordsArrayHandle::ReadPortalType screenCoordsP = screenCoords.ReadPortal();
TextureCoordsArrayHandle::ReadPortalType textureCoordsP = textureCoords.ReadPortal();
for (int i = 0; i < textLength; ++i)
{
this->ScreenCoords.push_back(screenCoordsP.Get(i));
this->TextureCoords.push_back(textureCoordsP.Get(i));
this->Colors.push_back(color.Components);
this->Depths.push_back(depth);
}
}
void TextRendererBatcher::Render(const vtkm::rendering::Canvas* canvas) const
{
ScreenCoordsArrayHandle screenCoords = vtkm::cont::make_ArrayHandle(this->ScreenCoords);
TextureCoordsArrayHandle textureCoords = vtkm::cont::make_ArrayHandle(this->TextureCoords);
vtkm::cont::ArrayHandle<ColorType> colors = vtkm::cont::make_ArrayHandle(this->Colors);
vtkm::cont::ArrayHandle<vtkm::Float32> depths = vtkm::cont::make_ArrayHandle(this->Depths);
vtkm::cont::TryExecute(RenderBitmapFontExecutor(screenCoords,
textureCoords,
this->FontTexture,
colors,
canvas->GetColorBuffer(),
canvas->GetDepthBuffer(),
canvas->GetWidth(),
canvas->GetHeight(),
depths));
}
}
} // namespace vtkm::rendering

@ -0,0 +1,65 @@
//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
//
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//============================================================================
#ifndef vtk_m_rendering_TextRendererBatcher_h
#define vtk_m_rendering_TextRendererBatcher_h
#include <string>
#include <vector>
#include <vtkm/rendering/BitmapFont.h>
#include <vtkm/rendering/Canvas.h>
#include <vtkm/rendering/Color.h>
#include <vtkm/rendering/vtkm_rendering_export.h>
namespace vtkm
{
namespace rendering
{
class VTKM_RENDERING_EXPORT TextRendererBatcher
{
public:
using FontTextureType = vtkm::rendering::Canvas::FontTextureType;
using ScreenCoordsType = vtkm::Id4;
using TextureCoordsType = vtkm::Vec4f_32;
using ColorType = vtkm::Vec4f_32;
using ScreenCoordsArrayHandle = vtkm::cont::ArrayHandle<ScreenCoordsType>;
using TextureCoordsArrayHandle = vtkm::cont::ArrayHandle<TextureCoordsType>;
using ColorsArrayHandle = vtkm::cont::ArrayHandle<ColorType>;
using DepthsArrayHandle = vtkm::cont::ArrayHandle<vtkm::Float32>;
/*
VTKM_CONT
TextRendererBatcher();
*/
VTKM_CONT
TextRendererBatcher(const vtkm::rendering::Canvas::FontTextureType& fontTexture);
VTKM_CONT
void BatchText(const ScreenCoordsArrayHandle& screenCoords,
const TextureCoordsArrayHandle& textureCoords,
const vtkm::rendering::Color& color,
const vtkm::Float32& depth);
void Render(const vtkm::rendering::Canvas* canvas) const;
private:
vtkm::rendering::Canvas::FontTextureType FontTexture;
std::vector<ScreenCoordsType> ScreenCoords;
std::vector<TextureCoordsType> TextureCoords;
std::vector<ColorType> Colors;
std::vector<vtkm::Float32> Depths;
};
}
} // namespace vtkm::rendering
#endif // vtk_m_rendering_TextRendererBatcher_h

@ -197,10 +197,14 @@ void View::RenderAnnotations()
{
this->SetupForScreenSpace();
this->RenderScreenAnnotations();
this->GetCanvas().BeginTextRenderingBatch();
for (auto& textAnnotation : this->Internal->TextAnnotations)
{
textAnnotation->Render(this->GetCamera(), this->GetWorldAnnotator(), this->GetCanvas());
}
this->GetCanvas().EndTextRenderingBatch();
for (auto& additionalAnnotation : this->Internal->AdditionalAnnotations)
{
additionalAnnotation();

@ -99,6 +99,8 @@ public:
virtual void RenderScreenAnnotations() = 0;
virtual void RenderWorldAnnotations() = 0;
void RenderAnnotations();
void SaveAs(const std::string& fileName) const;
VTKM_CONT VTKM_DEPRECATED(1.6, "Use ClearTextAnnotations Instead") void ClearAnnotations();
@ -126,7 +128,6 @@ protected:
void SetupForScreenSpace(bool viewportClip = false);
void RenderAnnotations();
vtkm::rendering::Color AxisColor = vtkm::rendering::Color::white;
bool WorldAnnotationsEnabled = true;

@ -56,6 +56,8 @@ void View2D::RenderScreenAnnotations()
viewportRight,
viewportBottom,
viewportTop);
this->GetCanvas().BeginTextRenderingBatch();
this->GetWorldAnnotator().BeginLineRenderingBatch();
this->HorizontalAxisAnnotation.SetColor(AxisColor);
this->HorizontalAxisAnnotation.SetScreenPosition(
viewportLeft, viewportBottom, viewportRight, viewportBottom);
@ -91,6 +93,8 @@ void View2D::RenderScreenAnnotations()
this->ColorBarAnnotation.Render(
this->GetCamera(), this->GetWorldAnnotator(), this->GetCanvas());
}
this->GetWorldAnnotator().EndLineRenderingBatch();
this->GetCanvas().EndTextRenderingBatch();
}
void View2D::RenderWorldAnnotations()

@ -47,17 +47,22 @@ void View3D::RenderScreenAnnotations()
{
if (this->GetScene().GetNumberOfActors() > 0)
{
this->GetCanvas().BeginTextRenderingBatch();
this->GetWorldAnnotator().BeginLineRenderingBatch();
//this->ColorBarAnnotation.SetAxisColor(vtkm::rendering::Color(1,1,1));
this->ColorBarAnnotation.SetFieldName(this->GetScene().GetActor(0).GetScalarField().GetName());
this->ColorBarAnnotation.SetRange(this->GetScene().GetActor(0).GetScalarRange(), 5);
this->ColorBarAnnotation.SetColorTable(this->GetScene().GetActor(0).GetColorTable());
this->ColorBarAnnotation.Render(
this->GetCamera(), this->GetWorldAnnotator(), this->GetCanvas());
this->GetWorldAnnotator().EndLineRenderingBatch();
this->GetCanvas().EndTextRenderingBatch();
}
}
void View3D::RenderWorldAnnotations()
{
this->GetCanvas().BeginTextRenderingBatch();
vtkm::Bounds bounds = this->GetScene().GetSpatialBounds();
vtkm::Float64 xmin = bounds.X.Min, xmax = bounds.X.Max;
vtkm::Float64 ymin = bounds.Y.Min, ymax = bounds.Y.Max;
@ -65,9 +70,11 @@ void View3D::RenderWorldAnnotations()
vtkm::Float64 dx = xmax - xmin, dy = ymax - ymin, dz = zmax - zmin;
vtkm::Float64 size = vtkm::Sqrt(dx * dx + dy * dy + dz * dz);
this->GetWorldAnnotator().BeginLineRenderingBatch();
this->BoxAnnotation.SetColor(Color(.5f, .5f, .5f));
this->BoxAnnotation.SetExtents(this->GetScene().GetSpatialBounds());
this->BoxAnnotation.Render(this->GetCamera(), this->GetWorldAnnotator());
this->GetWorldAnnotator().EndLineRenderingBatch();
vtkm::Vec3f_32 lookAt = this->GetCamera().GetLookAt();
vtkm::Vec3f_32 position = this->GetCamera().GetPosition();
@ -86,6 +93,7 @@ void View3D::RenderWorldAnnotations()
vtkm::Float64 yrel = vtkm::Abs(dy) / size;
vtkm::Float64 zrel = vtkm::Abs(dz) / size;
this->GetWorldAnnotator().BeginLineRenderingBatch();
this->XAxisAnnotation.SetAxis(0);
this->XAxisAnnotation.SetColor(AxisColor);
this->XAxisAnnotation.SetTickInvert(xtest, ytest, ztest);
@ -121,6 +129,9 @@ void View3D::RenderWorldAnnotations()
this->ZAxisAnnotation.SetLabelFontOffset(vtkm::Float32(size / 15.f));
this->ZAxisAnnotation.SetMoreOrLessTickAdjustment(zrel < .3 ? -1 : 0);
this->ZAxisAnnotation.Render(this->GetCamera(), this->GetWorldAnnotator(), this->GetCanvas());
this->GetWorldAnnotator().EndLineRenderingBatch();
this->GetCanvas().EndTextRenderingBatch();
}
}
} // namespace vtkm::rendering

@ -47,6 +47,7 @@ public:
private:
// 3D-specific annotations
vtkm::rendering::LineRendererBatcher LineBatcher;
vtkm::rendering::BoundingBoxAnnotation BoxAnnotation;
vtkm::rendering::AxisAnnotation3D XAxisAnnotation;
vtkm::rendering::AxisAnnotation3D YAxisAnnotation;

@ -32,10 +32,24 @@ void WorldAnnotator::AddLine(const vtkm::Vec3f_64& point0,
{
vtkm::Matrix<vtkm::Float32, 4, 4> transform =
vtkm::MatrixMultiply(Canvas->GetProjection(), Canvas->GetModelView());
LineRenderer renderer(Canvas, transform);
vtkm::rendering::WorldAnnotator* self = const_cast<vtkm::rendering::WorldAnnotator*>(this);
LineRenderer renderer(Canvas, transform, &(self->LineBatcher));
renderer.RenderLine(point0, point1, lineWidth, color);
}
void WorldAnnotator::BeginLineRenderingBatch() const
{
vtkm::rendering::WorldAnnotator* self = const_cast<vtkm::rendering::WorldAnnotator*>(this);
self->LineBatcher = vtkm::rendering::LineRendererBatcher();
}
void WorldAnnotator::EndLineRenderingBatch() const
{
vtkm::rendering::WorldAnnotator* self = const_cast<vtkm::rendering::WorldAnnotator*>(this);
vtkm::rendering::Canvas* canvas = const_cast<vtkm::rendering::Canvas*>(this->Canvas);
self->LineBatcher.Render(canvas);
}
void WorldAnnotator::AddText(const vtkm::Vec3f_32& origin,
const vtkm::Vec3f_32& right,
const vtkm::Vec3f_32& up,

@ -15,6 +15,7 @@
#include <vtkm/Types.h>
#include <vtkm/rendering/Canvas.h>
#include <vtkm/rendering/Color.h>
#include <vtkm/rendering/LineRendererBatcher.h>
namespace vtkm
{
@ -51,6 +52,12 @@ public:
vtkm::make_Vec(x0, y0, z0), vtkm::make_Vec(x1, y1, z1), lineWidth, color, inFront);
}
VTKM_CONT
void BeginLineRenderingBatch() const;
VTKM_CONT
void EndLineRenderingBatch() const;
virtual void AddText(const vtkm::Vec3f_32& origin,
const vtkm::Vec3f_32& right,
const vtkm::Vec3f_32& up,
@ -87,6 +94,7 @@ public:
private:
const vtkm::rendering::Canvas* Canvas;
vtkm::rendering::LineRendererBatcher LineBatcher;
};
}
} //namespace vtkm::rendering