Commit b1e3688b authored by Matt Larsen's avatar Matt Larsen
Browse files

multi-canvas working for ray tracer + volume renderer

parent 33af0fec
......@@ -349,6 +349,19 @@ void Camera::Print() const
std::cout << " Clip : " << GetClippingRange() << std::endl;
std::cout << " XyZ : " << Camera3D.XPan << " " << Camera3D.YPan << " " << Camera3D.Zoom
<< std::endl;
vtkm::Matrix<vtkm::Float32, 4, 4> pm, vm;
pm = CreateProjectionMatrix(512, 512);
vm = CreateViewMatrix();
std::cout << " PM: " << std::endl;
std::cout << pm[0][0] << " " << pm[0][1] << " " << pm[0][2] << " " << pm[0][3] << std::endl;
std::cout << pm[1][0] << " " << pm[1][1] << " " << pm[1][2] << " " << pm[1][3] << std::endl;
std::cout << pm[2][0] << " " << pm[2][1] << " " << pm[2][2] << " " << pm[2][3] << std::endl;
std::cout << pm[3][0] << " " << pm[3][1] << " " << pm[3][2] << " " << pm[3][3] << std::endl;
std::cout << " VM: " << std::endl;
std::cout << vm[0][0] << " " << vm[0][1] << " " << vm[0][2] << " " << vm[0][3] << std::endl;
std::cout << vm[1][0] << " " << vm[1][1] << " " << vm[1][2] << " " << vm[1][3] << std::endl;
std::cout << vm[2][0] << " " << vm[2][1] << " " << vm[2][2] << " " << vm[2][3] << std::endl;
std::cout << vm[3][0] << " " << vm[3][1] << " " << vm[3][2] << " " << vm[3][3] << std::endl;
}
else if (Mode == MODE_2D)
{
......
......@@ -38,20 +38,18 @@ namespace internal
class ClearBuffers : public vtkm::worklet::WorkletMapField
{
vtkm::rendering::Color ClearColor;
public:
VTKM_CONT
ClearBuffers(const vtkm::rendering::Color& clearColor)
: ClearColor(clearColor)
{
}
ClearBuffers() {}
typedef void ControlSignature(FieldOut<>, FieldOut<>);
typedef void ExecutionSignature(_1, _2);
VTKM_EXEC
void operator()(vtkm::Vec<vtkm::Float32, 4>& color, vtkm::Float32& depth) const
{
color = this->ClearColor.Components;
color[0] = 0.f;
color[1] = 0.f;
color[2] = 0.f;
color[3] = 0.f;
depth = 1.001f;
}
}; //class ClearBuffers
......@@ -66,10 +64,8 @@ struct ClearBuffersInvokeFunctor
DepthBufferType DepthBuffer;
VTKM_CONT
ClearBuffersInvokeFunctor(const vtkm::rendering::Color& backgroundColor,
const ColorBufferType& colorBuffer,
const DepthBufferType& depthBuffer)
: Worklet(backgroundColor)
ClearBuffersInvokeFunctor(const ColorBufferType& colorBuffer, const DepthBufferType& depthBuffer)
: Worklet()
, ColorBuffer(colorBuffer)
, DepthBuffer(depthBuffer)
{
......@@ -86,6 +82,59 @@ struct ClearBuffersInvokeFunctor
}
};
class BlendBackground : public vtkm::worklet::WorkletMapField
{
vtkm::Vec<vtkm::Float32, 4> BackgroundColor;
public:
VTKM_CONT
BlendBackground(const vtkm::Vec<vtkm::Float32, 4>& backgroundColor)
: BackgroundColor(backgroundColor)
{
}
typedef void ControlSignature(FieldInOut<>);
typedef void ExecutionSignature(_1);
VTKM_EXEC void operator()(vtkm::Vec<vtkm::Float32, 4>& color) const
{
if (color[3] >= 1.f)
return;
vtkm::Float32 alpha = BackgroundColor[3] * (1.f - color[3]);
color[0] = color[0] + BackgroundColor[0] * alpha;
color[1] = color[1] + BackgroundColor[1] * alpha;
color[2] = color[2] + BackgroundColor[2] * alpha;
color[3] = alpha + color[3];
}
}; //class BlendBackground
struct BlendBackgroundFunctor
{
typedef vtkm::rendering::Canvas::ColorBufferType ColorBufferType;
ColorBufferType ColorBuffer;
BlendBackground Worklet;
VTKM_CONT
BlendBackgroundFunctor(const ColorBufferType& colorBuffer,
const vtkm::Vec<vtkm::Float32, 4>& backgroundColor)
: ColorBuffer(colorBuffer)
, Worklet(backgroundColor)
{
}
template <typename Device>
VTKM_CONT bool operator()(Device) const
{
VTKM_IS_DEVICE_ADAPTER_TAG(Device);
vtkm::worklet::DispatcherMapField<BlendBackground, Device> dispatcher(this->Worklet);
dispatcher.Invoke(this->ColorBuffer);
return true;
}
};
class SurfaceConverter : public vtkm::worklet::WorkletMapField
{
vtkm::Matrix<vtkm::Float32, 4, 4> ViewProjMat;
......@@ -95,6 +144,7 @@ public:
SurfaceConverter(const vtkm::Matrix<vtkm::Float32, 4, 4> viewProjMat)
: ViewProjMat(viewProjMat)
{
vtkm::Matrix<vtkm::Float32, 4, 4> p = ViewProjMat;
}
typedef void ControlSignature(FieldIn<>,
......@@ -138,10 +188,23 @@ public:
color[1] = static_cast<vtkm::Float32>(colorBufferIn.Get(index * 4 + 1));
color[2] = static_cast<vtkm::Float32>(colorBufferIn.Get(index * 4 + 2));
color[3] = static_cast<vtkm::Float32>(colorBufferIn.Get(index * 4 + 3));
// blend the mapped color with existing canvas color
vtkm::Vec<vtkm::Float32, 4> inColor = colorBuffer.Get(pixelIndex);
vtkm::Float32 alpha = inColor[3] * (1.f - color[3]);
color[0] = color[0] + inColor[0] * alpha;
color[1] = color[1] + inColor[1] * alpha;
color[2] = color[2] + inColor[2] * alpha;
color[3] = alpha + color[3];
// clamp
for (vtkm::Int32 i = 0; i < 4; ++i)
{
color[i] = vtkm::Min(1.f, vtkm::Max(color[i], 0.f));
}
// The existng depth should already been feed into thge ray mapper
// so no color contribution will exist past the existing depth.
//vtkm::Float32 existingDepth = depthBuffer.Get(pixelIndex);
//std::cout<<" in "<<inDepth<<" "<<depth;;
depthBuffer.Set(pixelIndex, depth);
colorBuffer.Set(pixelIndex, color);
}
......@@ -196,7 +259,6 @@ VTKM_CONT void WriteToCanvas(const vtkm::rendering::raytracing::Ray<Precision>&
const vtkm::rendering::Camera& camera,
vtkm::rendering::CanvasRayTracer* canvas)
{
camera.Print();
WriteFunctor<Precision> functor(canvas, rays, colors, camera);
vtkm::cont::TryExecute(functor);
......@@ -232,12 +294,18 @@ void CanvasRayTracer::Finish()
// Nothing to finish
}
void CanvasRayTracer::BlendBackground()
{
vtkm::cont::TryExecute(internal::BlendBackgroundFunctor(this->GetColorBuffer(),
this->GetBackgroundColor().Components));
}
void CanvasRayTracer::Clear()
{
// TODO: Should the rendering library support policies or some other way to
// configure with custom devices?
vtkm::cont::TryExecute(internal::ClearBuffersInvokeFunctor(
this->GetBackgroundColor(), this->GetColorBuffer(), this->GetDepthBuffer()));
vtkm::cont::TryExecute(
internal::ClearBuffersInvokeFunctor(this->GetColorBuffer(), this->GetDepthBuffer()));
}
void CanvasRayTracer::WriteToCanvas(const vtkm::rendering::raytracing::Ray<vtkm::Float32>& rays,
......
......@@ -47,6 +47,8 @@ public:
vtkm::rendering::Canvas* NewCopy() const VTKM_OVERRIDE;
void BlendBackground();
void WriteToCanvas(const vtkm::rendering::raytracing::Ray<vtkm::Float32>& rays,
const vtkm::cont::ArrayHandle<vtkm::Float32>& colors,
const vtkm::rendering::Camera& camera);
......
......@@ -129,21 +129,16 @@ void MapperVolume::RenderCells(const vtkm::cont::DynamicCellSet& cellset,
coords, scalarField, cellset.Cast<vtkm::cont::CellSetStructured<3>>(), scalarRange);
tracer.SetColorMap(this->ColorMap);
tracer.SetBackgroundColor(this->Internals->Canvas->GetBackgroundColor().Components);
bool doComposite = this->Internals->CompositeBackground;
if (doComposite)
{
tracer.EnableCompositeBackground();
}
else
{
tracer.DisableCompositeBackground();
}
tracer.Render(rays);
timer.Reset();
this->Internals->Canvas->WriteToCanvas(rays, 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();
......
......@@ -55,21 +55,24 @@ public:
}
}; //class RayStatusFileter
class RayMapMaxDistances : public vtkm::worklet::WorkletMapField
class RayMapCanvas : public vtkm::worklet::WorkletMapField
{
protected:
vtkm::Matrix<vtkm::Float32, 4, 4> InverseProjection;
vtkm::Matrix<vtkm::Float32, 4, 4> InverseProjView;
vtkm::Id Width;
vtkm::Float32 DoubleInvHeight;
vtkm::Float32 DoubleInvWidth;
vtkm::Vec<vtkm::Float32, 3> Origin;
public:
VTKM_CONT
RayMapMaxDistances(const vtkm::Matrix<vtkm::Float32, 4, 4>& inverseProjection,
const vtkm::Id width,
const vtkm::Id height)
: InverseProjection(inverseProjection)
RayMapCanvas(const vtkm::Matrix<vtkm::Float32, 4, 4>& inverseProjView,
const vtkm::Id width,
const vtkm::Id height,
const vtkm::Vec<vtkm::Float32, 3>& origin)
: InverseProjView(inverseProjView)
, Width(width)
, Origin(origin)
{
VTKM_ASSERT(width > 0);
VTKM_ASSERT(height > 0);
......@@ -77,23 +80,13 @@ public:
DoubleInvWidth = 2.f / static_cast<vtkm::Float32>(width);
}
typedef void ControlSignature(FieldIn<>,
FieldInOut<>,
WholeArrayIn<>,
WholeArrayInOut<>,
WholeArrayIn<>);
typedef void ExecutionSignature(_1, _2, _3, _4, _5, WorkIndex);
template <typename Precision,
typename DepthPortalType,
typename RayColorType,
typename ColorBufferType>
typedef void ControlSignature(FieldIn<>, FieldInOut<>, WholeArrayIn<>);
typedef void ExecutionSignature(_1, _2, _3);
template <typename Precision, typename DepthPortalType>
VTKM_EXEC void operator()(const vtkm::Id& pixelId,
Precision& maxDistance,
const DepthPortalType& depths,
RayColorType& outColors,
const ColorBufferType inColors,
const vtkm::Id& index) const
const DepthPortalType& depths) const
{
vtkm::Vec<vtkm::Float32, 4> position;
position[0] = static_cast<vtkm::Float32>(pixelId % Width);
......@@ -104,24 +97,21 @@ public:
position[0] = position[0] * DoubleInvWidth - 1.f;
position[1] = position[1] * DoubleInvHeight - 1.f;
position[2] = 2.f * position[2] - 1.f;
position = vtkm::MatrixMultiply(InverseProjection, position);
//for(vtkm::Int32 i = 0; i < 4; ++i)
//{
// position[] /= position[3];
//}
maxDistance = -position[2] / position[3];
vtkm::Vec<vtkm::Float32, 4> inColor = inColors.Get(pixelId);
std::cout << "$" << inColor;
outColors.Set(index * 4 + 0, inColor[0]);
outColors.Set(index * 4 + 1, inColor[1]);
outColors.Set(index * 4 + 2, inColor[2]);
outColors.Set(index * 4 + 3, inColor[3]);
// offset so we don't go all the way to the same point
position[2] -= 0.00001f;
position = vtkm::MatrixMultiply(InverseProjView, position);
vtkm::Vec<vtkm::Float32, 3> p;
p[0] = position[0] / position[3];
p[1] = position[1] / position[3];
p[2] = position[2] / position[3];
p = p - Origin;
;
maxDistance = vtkm::Magnitude(p);
}
}; //class RayMapMinDistances
} // namespace detail
class RayOperations
{
public:
......
......@@ -364,7 +364,6 @@ public:
BOUNDS_CHECK(colorBuffer, pixelIndex * 4 + 3);
color[3] = colorBuffer.Get(pixelIndex * 4 + 3);
std::cout << color << " ";
if (minDistance == -1.f)
{
return; //TODO: Compact? or just image subset...
......@@ -697,83 +696,22 @@ public:
maxDistance = vtkm::Min(maxDistance, exitDistance);
if (maxDistance < minDistance)
{
//std::cout<<" & "<<maxDistance<<" <"<<minDistance<<" "<<exitDistance<<">";
minDistance = -1.f; //flag for miss
}
else
{
distance = minDistance;
std::cout << " | " << maxDistance << " <" << minDistance << " " << exitDistance << ">";
}
}
}; //class CalcRayStart
class CompositeBackground : public vtkm::worklet::WorkletMapField
{
vtkm::Vec<vtkm::Float32, 4> BackgroundColor;
public:
VTKM_CONT
CompositeBackground(const vtkm::Vec<vtkm::Float32, 4>& backgroundColor)
: BackgroundColor(backgroundColor)
{
}
typedef void ControlSignature(FieldIn<>, WholeArrayInOut<>);
typedef void ExecutionSignature(_1, _2);
template <typename ColorBufferType>
VTKM_EXEC void operator()(const vtkm::Id& pixelIndex, ColorBufferType& colorBuffer) const
{
vtkm::Vec<vtkm::Float32, 4> color;
BOUNDS_CHECK(colorBuffer, pixelIndex * 4 + 0);
color[0] = colorBuffer.Get(pixelIndex * 4 + 0);
BOUNDS_CHECK(colorBuffer, pixelIndex * 4 + 1);
color[1] = colorBuffer.Get(pixelIndex * 4 + 1);
BOUNDS_CHECK(colorBuffer, pixelIndex * 4 + 2);
color[2] = colorBuffer.Get(pixelIndex * 4 + 2);
BOUNDS_CHECK(colorBuffer, pixelIndex * 4 + 3);
color[3] = colorBuffer.Get(pixelIndex * 4 + 3);
if (color[3] >= 1.f)
return;
vtkm::Float32 alpha = BackgroundColor[3] * (1.f - color[3]);
color[0] = color[0] + BackgroundColor[0] * alpha;
color[1] = color[1] + BackgroundColor[1] * alpha;
color[2] = color[2] + BackgroundColor[2] * alpha;
color[3] = alpha + color[3];
BOUNDS_CHECK(colorBuffer, pixelIndex * 4 + 0);
colorBuffer.Set(pixelIndex * 4 + 0, color[0]);
BOUNDS_CHECK(colorBuffer, pixelIndex * 4 + 1);
colorBuffer.Set(pixelIndex * 4 + 1, color[1]);
BOUNDS_CHECK(colorBuffer, pixelIndex * 4 + 2);
colorBuffer.Set(pixelIndex * 4 + 2, color[2]);
BOUNDS_CHECK(colorBuffer, pixelIndex * 4 + 3);
colorBuffer.Set(pixelIndex * 4 + 3, color[3]);
}
}; //class CompositeBackground
VolumeRendererStructured::VolumeRendererStructured()
{
IsSceneDirty = false;
DoCompositeBackground = true;
IsUniformDataSet = true;
SampleDistance = -1.f;
}
void VolumeRendererStructured::EnableCompositeBackground()
{
DoCompositeBackground = true;
}
void VolumeRendererStructured::DisableCompositeBackground()
{
DoCompositeBackground = false;
}
void VolumeRendererStructured::SetColorMap(
const vtkm::cont::ArrayHandle<vtkm::Vec<vtkm::Float32, 4>>& colorMap)
{
......@@ -926,17 +864,6 @@ void VolumeRendererStructured::RenderOnDevice(vtkm::rendering::raytracing::Ray<P
logger->AddLogData("sample", time);
timer.Reset();
if (DoCompositeBackground)
{
vtkm::cont::ArrayHandleCounting<vtkm::Id> rayIterator(0, 1, rays.NumRays);
vtkm::worklet::DispatcherMapField<CompositeBackground>(CompositeBackground(BackgroundColor))
.Invoke(rayIterator, rays.Buffers.at(0).Buffer);
time = timer.GetElapsedTime();
logger->AddLogData("compsosite_background", time);
timer.Reset();
}
time = renderTimer.GetElapsedTime();
logger->CloseLogEntry(time);
} //Render
......
......@@ -80,7 +80,6 @@ protected:
struct RenderFunctor;
bool IsSceneDirty;
bool DoCompositeBackground;
bool IsUniformDataSet;
vtkm::Bounds SpatialExtent;
vtkm::cont::DynamicArrayHandleCoordinateSystem Coordinates;
......
//============================================================================
// 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 2015 Sandia Corporation.
// Copyright 2015 UT-Battelle, LLC.
// Copyright 2015 Los Alamos National Security.
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// 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/cont/DeviceAdapter.h>
#include <vtkm/cont/testing/MakeTestDataSet.h>
#include <vtkm/cont/testing/Testing.h>
#include <vtkm/rendering/Actor.h>
#include <vtkm/rendering/Canvas.h>
#include <vtkm/rendering/CanvasRayTracer.h>
#include <vtkm/rendering/MapperRayTracer.h>
#include <vtkm/rendering/MapperVolume.h>
#include <vtkm/rendering/Scene.h>
#include <vtkm/rendering/View3D.h>
#include <vtkm/rendering/testing/RenderTest.h>
#include <vtkm/rendering/raytracing/RayOperations.h>
namespace
{
void RenderTests()
{
typedef vtkm::rendering::MapperVolume M;
typedef vtkm::rendering::MapperRayTracer R;
typedef vtkm::rendering::CanvasRayTracer C;
typedef vtkm::rendering::View3D V3;
vtkm::cont::testing::MakeTestDataSet maker;
vtkm::rendering::ColorTable colorTable("thermal");
vtkm::rendering::ColorTable colorTable2("cool2warm");
colorTable2.AddAlphaControlPoint(0.0, .02);
colorTable2.AddAlphaControlPoint(1.0, .02);
vtkm::rendering::testing::MultiMapperRender<R, M, C, V3>(maker.Make3DExplicitDataSet4(),
maker.Make3DRectilinearDataSet0(),
"pointvar",
colorTable,
colorTable2,
"multi.pnm");
}
} //namespace
int UnitTestMultiMapper(int, char* [])
{
return vtkm::cont::testing::Testing::Run(RenderTests);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment