Skip to content
3 changes: 2 additions & 1 deletion application/F3DOptionsTools.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ static inline const std::array<CLIGroup, 8> CLIOptions = {{
{"anti-aliasing", "a", R"(Select anti-aliasing method ("none", "fxaa", "ssaa" or "taa"))", "<string>", "fxaa"},
{"anti-aliasing-mode", "", R"(Select anti-aliasing method ("fxaa", "ssaa" or "taa") (deprecated))", "<string>", "fxaa"},
{"tone-mapping", "t", "Enable Tone Mapping, providing balanced coloring", "<bool>", "1"},
{"final-shader", "", "Execute the final shader at the end of the rendering pipeline", "<GLSL code>", ""} } },
{"final-shader", "", "Execute the final shader at the end of the rendering pipeline", "<GLSL code>", ""},
{"display-depth", "", "Display depth buffer as grayscale image", "<bool>", "1"} } },
{"Testing",
{ {"reference", "", "Reference", "<png file>", ""},
{"reference-threshold", "", "Testing threshold", "<threshold>", ""},
Expand Down
1 change: 1 addition & 0 deletions application/F3DOptionsTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ static inline const std::map<std::string_view, std::string_view> LibOptionsNames
{ "ambient-occlusion", "render.effect.ambient_occlusion" },
{ "tone-mapping", "render.effect.tone_mapping" },
{ "final-shader", "render.effect.final_shader" },
{ "display-depth", "render.effect.display_depth" },
{ "textures-transform", "model.textures_transform" },
};

Expand Down
4 changes: 4 additions & 0 deletions library/options.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@
},
"final_shader": {
"type": "string"
},
"display_depth": {
"type": "bool",
"default_value": "false"
}
},
"hdri": {
Expand Down
2 changes: 2 additions & 0 deletions library/src/window_impl.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,8 @@ void window_impl::UpdateDynamicOptions()
renderer->SetUseSSAOPass(opt.render.effect.ambient_occlusion);
renderer->SetAntiAliasingMode(aaMode);
renderer->SetUseToneMappingPass(opt.render.effect.tone_mapping);
renderer->SetDisplayDepth(opt.render.effect.display_depth);
renderer->SetDisplayDepthScalarColoring(opt.model.scivis.enable);
renderer->SetBlendingMode(blendMode);
renderer->SetBackfaceType(opt.render.backface_type);
renderer->SetFinalShader(opt.render.effect.final_shader);
Expand Down
1 change: 1 addition & 0 deletions vtkext/private/module/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ set(classes
vtkF3DUIActor
vtkF3DUserRenderPass
vtkF3DTAAResolvePass
vtkF3DDisplayDepthRenderPass
)

if (F3D_MODULE_UI)
Expand Down
187 changes: 187 additions & 0 deletions vtkext/private/module/vtkF3DDisplayDepthRenderPass.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
#include "vtkF3DDisplayDepthRenderPass.h"

#include <vtkObjectFactory.h>
#include <vtkOpenGLError.h>
#include <vtkOpenGLFramebufferObject.h>
#include <vtkOpenGLQuadHelper.h>
#include <vtkOpenGLRenderUtilities.h>
#include <vtkOpenGLRenderWindow.h>
#include <vtkOpenGLRenderer.h>
#include <vtkOpenGLShaderCache.h>
#include <vtkOpenGLState.h>
#include <vtkRenderState.h>
#include <vtkRenderer.h>
#include <vtkShaderProgram.h>
#include <vtkTextureObject.h>
#include <vtk_glew.h>

vtkStandardNewMacro(vtkF3DDisplayDepthRenderPass);

//------------------------------------------------------------------------------
void vtkF3DDisplayDepthRenderPass::Render(const vtkRenderState* state)
{
vtkOpenGLClearErrorMacro();
this->NumberOfRenderedProps = 0;

vtkRenderer* renderer = state->GetRenderer();
vtkOpenGLRenderWindow* renWin = vtkOpenGLRenderWindow::SafeDownCast(renderer->GetRenderWindow());
vtkOpenGLState* ostate = renWin->GetState();

vtkOpenGLState::ScopedglEnableDisable bsaver(ostate, GL_BLEND);
vtkOpenGLState::ScopedglEnableDisable dsaver(ostate, GL_DEPTH_TEST);

assert(this->DelegatePass != nullptr);

int pos[2];
int size[2];
renderer->GetTiledSizeAndOrigin(&size[0], &size[1], &pos[0], &pos[1]);

InitializeResources(renWin, size[0], size[1]);

// render to color and depth texture
renWin->GetState()->PushFramebufferBindings();
this->RenderDelegate(state, size[0], size[1], size[0], size[1], this->FrameBufferObject,
this->ColorTexture, this->DepthTexture);
renWin->GetState()->PopFramebufferBindings();

// depth displaying pass
if (!this->QuadHelper)
{
std::string depthDisplayingFS =
vtkOpenGLRenderUtilities::GetFullScreenQuadFragmentShaderTemplate();
vtkShaderProgram::Substitute(depthDisplayingFS, "//VTK::FSQ::Decl",
"uniform sampler2D texDepth;\n"
"uniform bool useColorMap;\n"
"uniform sampler1D colorMapTexture;\n"
"//VTK::FSQ::Decl");

vtkShaderProgram::Substitute(depthDisplayingFS, "//VTK::FSQ::Impl",
"float depth = texture2D(texDepth, texCoord).r;\n"
"if (useColorMap)\n"
"{\n"
" vec4 colorMapped = texture1D(colorMapTexture, depth);\n"
" gl_FragData[0] = vec4(colorMapped.rgb, 1.0);\n"
" return;\n"
"}\n"
"gl_FragData[0] = vec4(depth, depth, depth, 1.0);\n"
"//VTK::FSQ::Impl");
this->QuadHelper =
std::make_shared<vtkOpenGLQuadHelper>(renWin, nullptr, depthDisplayingFS.c_str(), nullptr);
this->QuadHelper->ShaderChangeValue = this->GetMTime();
}
else
{
renWin->GetShaderCache()->ReadyShaderProgram(this->QuadHelper->Program);
}

assert(this->QuadHelper->Program && this->QuadHelper->Program->GetCompiled());

this->ColorTexture->Activate();
this->DepthTexture->Activate();
this->ColorMapTexture->Activate();

this->QuadHelper->Program->SetUniformi("texDepth", this->DepthTexture->GetTextureUnit());
this->QuadHelper->Program->SetUniformi("useColorMap", this->ColorMap != nullptr ? 1 : 0);
this->QuadHelper->Program->SetUniformi(
"colorMapTexture", this->ColorMapTexture->GetTextureUnit());

ostate->vtkglEnable(GL_DEPTH_TEST);
ostate->vtkglDepthFunc(GL_LEQUAL);
ostate->vtkglClear(GL_DEPTH_BUFFER_BIT);

this->QuadHelper->Render();

this->ColorTexture->Deactivate();
this->DepthTexture->Deactivate();
this->ColorMapTexture->Deactivate();
vtkOpenGLCheckErrorMacro("failed after Render");
}

void vtkF3DDisplayDepthRenderPass::InitializeResources(vtkOpenGLRenderWindow* renWin, int w, int h)
{
if (this->DepthTexture == nullptr)
{
this->DepthTexture = vtkSmartPointer<vtkTextureObject>::New();
this->DepthTexture->SetContext(renWin);
this->DepthTexture->AllocateDepth(w, h, vtkTextureObject::Float32);
}
this->DepthTexture->Resize(w, h);

if (this->ColorTexture == nullptr)
{
this->ColorTexture = vtkSmartPointer<vtkTextureObject>::New();
this->ColorTexture->SetContext(renWin);
this->ColorTexture->SetFormat(GL_RGBA);
this->ColorTexture->SetInternalFormat(GL_RGBA16F);
this->ColorTexture->SetDataType(GL_HALF_FLOAT);
this->ColorTexture->SetMinificationFilter(vtkTextureObject::Linear);
this->ColorTexture->SetMagnificationFilter(vtkTextureObject::Linear);
this->ColorTexture->SetWrapS(vtkTextureObject::ClampToEdge);
this->ColorTexture->SetWrapT(vtkTextureObject::ClampToEdge);
this->ColorTexture->Allocate2D(w, h, 4, VTK_FLOAT);
}
this->ColorTexture->Resize(w, h);

if (this->ColorMapTexture == nullptr)
{
this->ColorMapTexture = vtkSmartPointer<vtkTextureObject>::New();
this->ColorMapTexture->SetContext(renWin);
this->ColorMapTexture->Allocate1D(256, 3, VTK_FLOAT);
this->ColorMapTexture->SetWrapS(vtkTextureObject::ClampToEdge);
this->ColorMapTexture->SetWrapT(vtkTextureObject::ClampToEdge);
this->ColorMapTexture->SetMinificationFilter(vtkTextureObject::Linear);
this->ColorMapTexture->SetMagnificationFilter(vtkTextureObject::Linear);

this->ColorMapTexture->SetFormat(GL_RGB);
this->ColorMapTexture->SetInternalFormat(GL_RGB16F);
this->ColorMapTexture->SetDataType(GL_FLOAT);

if (this->ColorMap && this->ColorMapBuildTime.GetMTime() < this->ColorMap->GetMTime())
{
constexpr int tableSize = 256;
this->ColorMap->SetNumberOfValues(tableSize);
this->ColorMap->Build();
double range[2];
this->ColorMap->GetRange(range);

std::vector<float> table(tableSize * 3);
this->ColorMap->GetTable(range[0], range[1], tableSize, table.data());

this->ColorMapTexture->Create1DFromRaw(tableSize, 3, VTK_FLOAT, table.data());
this->ColorMapBuildTime.Modified();
}
}

if (this->FrameBufferObject == nullptr)
{
this->FrameBufferObject = vtkSmartPointer<vtkOpenGLFramebufferObject>::New();
this->FrameBufferObject->SetContext(renWin);
}
}

void vtkF3DDisplayDepthRenderPass::ReleaseGraphicsResources(vtkWindow* window)
{
this->Superclass::ReleaseGraphicsResources(window);

if (this->FrameBufferObject)
{
this->FrameBufferObject->ReleaseGraphicsResources(window);
}
if (this->ColorTexture)
{
this->ColorTexture->ReleaseGraphicsResources(window);
}
if (this->DepthTexture)
{
this->DepthTexture->ReleaseGraphicsResources(window);
}
if (this->ColorMapTexture)
{
this->ColorMapTexture->ReleaseGraphicsResources(window);
}
}

void vtkF3DDisplayDepthRenderPass::SetColorMap(vtkDiscretizableColorTransferFunction* colorMap)
{
this->ColorMap = colorMap;
}
41 changes: 41 additions & 0 deletions vtkext/private/module/vtkF3DDisplayDepthRenderPass.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#ifndef vtkF3DDepthRenderPass_h
#define vtkF3DDepthRenderPass_h

#include "vtkImageProcessingPass.h"

#include <vtkDepthImageProcessingPass.h>
#include <vtkDiscretizableColorTransferFunction.h>
#include <vtkSmartPointer.h>

#include <memory>

class vtkOpenGLFramebufferObject;
class vtkOpenGLQuadHelper;
class vtkTextureObject;

class vtkF3DDisplayDepthRenderPass : public vtkDepthImageProcessingPass
{
public:
static vtkF3DDisplayDepthRenderPass* New();
vtkTypeMacro(vtkF3DDisplayDepthRenderPass, vtkDepthImageProcessingPass);
void Render(const vtkRenderState* state) override;
void ReleaseGraphicsResources(vtkWindow* window) override;

void SetColorMap(vtkDiscretizableColorTransferFunction* colorMap);

private:
vtkF3DDisplayDepthRenderPass() = default;
~vtkF3DDisplayDepthRenderPass() override = default;

void InitializeResources(vtkOpenGLRenderWindow* renWin, int w, int h);

vtkSmartPointer<vtkDiscretizableColorTransferFunction> ColorMap = nullptr;
vtkTimeStamp ColorMapBuildTime;

vtkSmartPointer<vtkTextureObject> ColorTexture;
vtkSmartPointer<vtkTextureObject> DepthTexture;
vtkSmartPointer<vtkTextureObject> ColorMapTexture;
vtkSmartPointer<vtkOpenGLFramebufferObject> FrameBufferObject;
std::shared_ptr<vtkOpenGLQuadHelper> QuadHelper;
};
#endif
44 changes: 43 additions & 1 deletion vtkext/private/module/vtkF3DRenderer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "F3DLog.h"
#include "vtkF3DCachedLUTTexture.h"
#include "vtkF3DCachedSpecularTexture.h"
#include "vtkF3DDisplayDepthRenderPass.h"
#include "vtkF3DInteractorStyle.h"
#include "vtkF3DOpenGLGridMapper.h"
#include "vtkF3DOverlayRenderPass.h"
Expand All @@ -19,6 +20,7 @@
#include <vtkCamera.h>
#include <vtkCameraOrientationRepresentation.h>
#include <vtkCameraOrientationWidget.h>
#include <vtkCameraPass.h>
#include <vtkCellData.h>
#include <vtkCornerAnnotation.h>
#include <vtkCullerCollection.h>
Expand All @@ -35,6 +37,7 @@
#include <vtkMatrix4x4.h>
#include <vtkMultiBlockDataSet.h>
#include <vtkObjectFactory.h>
#include <vtkOpaquePass.h>
#include <vtkOpenGLFXAAPass.h>
#include <vtkOpenGLRenderWindow.h>
#include <vtkOpenGLRenderer.h>
Expand Down Expand Up @@ -481,6 +484,22 @@ void vtkF3DRenderer::ConfigureRenderPasses()
// Image post processing passes
vtkSmartPointer<vtkRenderPass> renderingPass = newPass;

if (this->DisplayDepth)
{
// discard vtkF3DRenderPass if displaying depth
vtkNew<vtkOpaquePass> opaqueP;
vtkNew<vtkCameraPass> camP;
vtkNew<vtkF3DDisplayDepthRenderPass> depthP;
camP->SetDelegatePass(opaqueP);
depthP->SetDelegatePass(camP);
if (this->DisplayDepthScalarColoring)
{
this->ConfigureColoring();
depthP->SetColorMap(this->ColorTransferFunction);
}
renderingPass = depthP;
}

if (this->AntiAliasingModeEnabled == vtkF3DRenderer::AntiAliasingMode::SSAA)
{
vtkNew<vtkSSAAPass> ssaaP;
Expand Down Expand Up @@ -1730,6 +1749,28 @@ void vtkF3DRenderer::SetUseToneMappingPass(bool use)
}
}

//----------------------------------------------------------------------------
void vtkF3DRenderer::SetDisplayDepth(bool use)
{
if (this->DisplayDepth != use)
{
this->DisplayDepth = use;
this->RenderPassesConfigured = false;
this->CheatSheetConfigured = false;
}
}

//----------------------------------------------------------------------------
void vtkF3DRenderer::SetDisplayDepthScalarColoring(bool use)
{
if (this->DisplayDepthScalarColoring != use)
{
this->DisplayDepthScalarColoring = use;
this->RenderPassesConfigured = false;
this->CheatSheetConfigured = false;
}
}

//----------------------------------------------------------------------------
void vtkF3DRenderer::SetUseRaytracing(bool use)
{
Expand Down Expand Up @@ -2836,7 +2877,8 @@ void vtkF3DRenderer::ConfigureColoring()
assert(this->Importer);

// Recover coloring information and update handler
bool enableColoring = this->EnableColoring || (!this->UseRaytracing && this->UseVolume);
bool enableColoring = this->EnableColoring || (!this->UseRaytracing && this->UseVolume) ||
(this->DisplayDepth && this->DisplayDepthScalarColoring);
F3DColoringInfoHandler& coloringHandler = this->Importer->GetColoringInfoHandler();
auto info = coloringHandler.SetCurrentColoring(
enableColoring, this->UseCellColoring, this->ArrayNameForColoring, false);
Expand Down
4 changes: 4 additions & 0 deletions vtkext/private/module/vtkF3DRenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ class vtkF3DRenderer : public vtkOpenGLRenderer
void SetUseSSAOPass(bool use);
void SetAntiAliasingMode(AntiAliasingMode mode);
void SetUseToneMappingPass(bool use);
void SetDisplayDepth(bool use);
void SetDisplayDepthScalarColoring(bool use);
void SetUseBlurBackground(bool use);
void SetBlurCircleOfConfusionRadius(double radius);
void SetRaytracingSamples(int samples);
Expand Down Expand Up @@ -689,6 +691,8 @@ class vtkF3DRenderer : public vtkOpenGLRenderer
BlendingMode BlendingModeEnabled = BlendingMode::NONE;
bool UseSSAOPass = false;
bool UseToneMappingPass = false;
bool DisplayDepth = false;
bool DisplayDepthScalarColoring = false;
bool UseBlurBackground = false;
std::optional<bool> UseOrthographicProjection = false;
bool UseTrackball = false;
Expand Down