vtk-m/vtkm/rendering/MapperQuad.cxx
Haocheng LIU 415252c662 Introduce asynchronous and device independent timer
The timer class now is asynchronous and device independent. it's using an
similiar API as vtkOpenGLRenderTimer with Start(), Stop(), Reset(), Ready(),
and GetElapsedTime() function. For convenience and backward compability, Each
Start() function call will call Reset() internally and each GetElapsedTime()
function call will call Stop() function if it hasn't been called yet for keeping
backward compatibility purpose.

Bascially it can be used in two modes:

* Create a Timer without any device info. vtkm::cont::Timer time;

  * It would enable timers for all enabled devices on the machine. Users can get a
specific elapsed time by passing a device id into the GetElapsedtime function.
If no device is provided, it would pick the maximum of all timer results - the
logic behind this decision is that if cuda is disabled, openmp, serial and tbb
roughly give the same results; if cuda is enabled it's safe to return the
maximum elapsed time since users are more interested in the device execution
time rather than the kernal launch time. The Ready function can be handy here
to query the status of the timer.

* Create a Timer with a device id. vtkm::cont::Timer time((vtkm::cont::DeviceAdapterTagCuda()));

  * It works as the old timer that times for a specific device id.
2019-02-05 12:01:56 -05:00

168 lines
4.8 KiB
C++

//============================================================================
// 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.
//
// Copyright 2016 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
// Copyright 2016 UT-Battelle, LLC.
// Copyright 2016 Los Alamos National Security.
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// the U.S. Government retains certain rights in this software.
//
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
// Laboratory (LANL), the U.S. Government retains certain rights in
// this software.
//============================================================================
#include <vtkm/rendering/MapperQuad.h>
#include <vtkm/cont/Timer.h>
#include <vtkm/cont/TryExecute.h>
#include <vtkm/rendering/CanvasRayTracer.h>
#include <vtkm/rendering/Cylinderizer.h>
#include <vtkm/rendering/raytracing/Camera.h>
#include <vtkm/rendering/raytracing/Logger.h>
#include <vtkm/rendering/raytracing/QuadExtractor.h>
#include <vtkm/rendering/raytracing/QuadIntersector.h>
#include <vtkm/rendering/raytracing/RayOperations.h>
#include <vtkm/rendering/raytracing/RayTracer.h>
namespace vtkm
{
namespace rendering
{
struct MapperQuad::InternalsType
{
vtkm::rendering::CanvasRayTracer* Canvas;
vtkm::rendering::raytracing::RayTracer Tracer;
vtkm::rendering::raytracing::Camera RayCamera;
vtkm::rendering::raytracing::Ray<vtkm::Float32> Rays;
bool CompositeBackground;
VTKM_CONT
InternalsType()
: Canvas(nullptr)
, CompositeBackground(true)
{
}
};
MapperQuad::MapperQuad()
: Internals(new InternalsType)
{
}
MapperQuad::~MapperQuad()
{
}
void MapperQuad::SetCanvas(vtkm::rendering::Canvas* canvas)
{
if (canvas != nullptr)
{
this->Internals->Canvas = dynamic_cast<CanvasRayTracer*>(canvas);
if (this->Internals->Canvas == nullptr)
{
throw vtkm::cont::ErrorBadValue("Ray Tracer: bad canvas type. Must be CanvasRayTracer");
}
}
else
{
this->Internals->Canvas = nullptr;
}
}
vtkm::rendering::Canvas* MapperQuad::GetCanvas() const
{
return this->Internals->Canvas;
}
void MapperQuad::RenderCells(const vtkm::cont::DynamicCellSet& cellset,
const vtkm::cont::CoordinateSystem& coords,
const vtkm::cont::Field& scalarField,
const vtkm::cont::ColorTable& vtkmNotUsed(colorTable),
const vtkm::rendering::Camera& camera,
const vtkm::Range& scalarRange)
{
raytracing::Logger* logger = raytracing::Logger::GetInstance();
logger->OpenLogEntry("mapper_ray_tracer");
vtkm::cont::Timer tot_timer;
tot_timer.Start();
vtkm::cont::Timer timer;
//
// Add supported shapes
//
vtkm::Bounds shapeBounds;
raytracing::QuadExtractor quadExtractor;
quadExtractor.ExtractCells(cellset);
if (quadExtractor.GetNumberOfQuads() > 0)
{
raytracing::QuadIntersector* quadIntersector = new raytracing::QuadIntersector();
quadIntersector->SetData(coords, quadExtractor.GetQuadIds());
this->Internals->Tracer.AddShapeIntersector(quadIntersector);
shapeBounds.Include(quadIntersector->GetShapeBounds());
}
//
// Create rays
//
vtkm::rendering::raytracing::Camera& cam = this->Internals->Tracer.GetCamera();
cam.SetParameters(camera, *this->Internals->Canvas);
this->Internals->RayCamera.SetParameters(camera, *this->Internals->Canvas);
this->Internals->RayCamera.CreateRays(this->Internals->Rays, shapeBounds);
this->Internals->Rays.Buffers.at(0).InitConst(0.f);
raytracing::RayOperations::MapCanvasToRays(
this->Internals->Rays, camera, *this->Internals->Canvas);
this->Internals->Tracer.SetField(scalarField, scalarRange);
this->Internals->Tracer.SetColorMap(this->ColorMap);
this->Internals->Tracer.Render(this->Internals->Rays);
timer.Start();
this->Internals->Canvas->WriteToCanvas(
this->Internals->Rays, this->Internals->Rays.Buffers.at(0).Buffer, camera);
if (this->Internals->CompositeBackground)
{
this->Internals->Canvas->BlendBackground();
}
vtkm::Float64 time = timer.GetElapsedTime();
logger->AddLogData("write_to_canvas", time);
time = tot_timer.GetElapsedTime();
logger->CloseLogEntry(time);
}
void MapperQuad::SetCompositeBackground(bool on)
{
this->Internals->CompositeBackground = on;
}
void MapperQuad::StartScene()
{
// Nothing needs to be done.
}
void MapperQuad::EndScene()
{
// Nothing needs to be done.
}
vtkm::rendering::Mapper* MapperQuad::NewCopy() const
{
return new vtkm::rendering::MapperQuad(*this);
}
}
} // vtkm::rendering