Skip to content

Commit

Permalink
Traktor: Generating HiZ in a power-of-two to ensure it's properly ali…
Browse files Browse the repository at this point in the history
…gned with world geometry. Overall cleanup.
  • Loading branch information
apistol78 committed Feb 22, 2024
1 parent 59e2775 commit bf8e450
Show file tree
Hide file tree
Showing 12 changed files with 241 additions and 203 deletions.
4 changes: 4 additions & 0 deletions code/Mesh/Instance/InstanceMesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace traktor::mesh

render::Handle s_handleInstanceWorld(L"InstanceWorld");
render::Handle s_handleInstanceWorldLast(L"InstanceWorldLast");
render::Handle s_handleTargetSize(L"InstanceMesh_TargetSize");
render::Handle s_handleBoundingBoxMin(L"InstanceMesh_BoundingBoxMin");
render::Handle s_handleBoundingBoxMax(L"InstanceMesh_BoundingBoxMax");
render::Handle s_handleVisibility(L"InstanceMesh_Visibility");
Expand Down Expand Up @@ -132,6 +133,8 @@ void InstanceMesh::build(
for (int32_t i = (int32_t)cf.planes.size(); i < sizeof_array(cullFrustum); ++i)
cullFrustum[i] = Vector4::zero();

const Vector2 viewSize = worldRenderView.getViewSize();

auto renderBlock = renderContext->allocNamed< render::ComputeRenderBlock >(
str(L"InstanceMesh cull %d", worldRenderView.getCascade())
);
Expand All @@ -155,6 +158,7 @@ void InstanceMesh::build(

worldRenderPass.setProgramParameters(renderBlock->programParams);

renderBlock->programParams->setVectorParameter(s_handleTargetSize, Vector4(viewSize.x, viewSize.y, 0.0f, 0.0f));
renderBlock->programParams->setVectorParameter(s_handleBoundingBoxMin, m_renderMesh->getBoundingBox().mn);
renderBlock->programParams->setVectorParameter(s_handleBoundingBoxMax, m_renderMesh->getBoundingBox().mx);
renderBlock->programParams->setVectorArrayParameter(s_handleCullFrustum, cullFrustum, sizeof_array(cullFrustum));
Expand Down
10 changes: 3 additions & 7 deletions code/Render/Frame/RenderPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,10 @@ void RenderPass::addInput(handle_t resourceId)
input.resourceId = resourceId;
}

bool RenderPass::requireInput(handle_t resourceId) const
void RenderPass::addWeakInput(handle_t resourceId)
{
for (const auto& input : m_inputs)
{
if (input.resourceId == resourceId)
return true;
}
return false;
// #todo Currently we can get away with not doing anything but
// we need to revisit this to ensure resource life time.
}

void RenderPass::setOutput(handle_t resourceId)
Expand Down
22 changes: 21 additions & 1 deletion code/Render/Frame/RenderPass.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,36 @@ class T_DLLCLASS RenderPass : public RefCountImpl< ITypedObject >

//! \{

/*! Add input to render pass.
*
* \param resourceId ID of input resource.
*/
void addInput(handle_t resourceId);

bool requireInput(handle_t resourceId) const;
/*! Add weak input to render pass.
*
* A weak input doesn't resolve into a dependency between
* passes but only register the pass as it will use the
* resource during build.
*
* \param resourceId ID of input resource.
*/
void addWeakInput(handle_t resourceId);

const StaticVector< Input, 16 >& getInputs() const { return m_inputs; }

//! \}

//! \{

/*! Set output resource of render pass.
*
* Resource ID 0 means the primary framebuffer and
* an ID of ~0 means the render pass doesn't have an
* output resource associated.
*
* \param resourceId ID of output resource.
*/
void setOutput(handle_t resourceId);

void setOutput(handle_t resourceId, uint32_t load, uint32_t store);
Expand Down
2 changes: 1 addition & 1 deletion code/Render/IRenderView.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* TRAKTOR
* Copyright (c) 2022 Anders Pistol.
* Copyright (c) 2022-2024 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down
60 changes: 30 additions & 30 deletions code/Render/Vulkan/RenderViewVk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1065,7 +1065,7 @@ void RenderViewVk::barrier(Stage from, Stage to, ITexture* written, uint32_t wri
0, nullptr
);
}
else if (from == Stage::Compute && to == Stage::Compute && written == nullptr)
else if (from == Stage::Compute && to == Stage::Compute/* && written == nullptr*/)
{
VkMemoryBarrier mb = {};
mb.sType = VK_STRUCTURE_TYPE_MEMORY_BARRIER;
Expand All @@ -1083,35 +1083,35 @@ void RenderViewVk::barrier(Stage from, Stage to, ITexture* written, uint32_t wri
0, nullptr
);
}
else if (from == Stage::Compute && to == Stage::Compute && written != nullptr)
{
const Image* img = mandatory_non_null_type_cast< TextureVk* >(written)->getImage();

VkImageMemoryBarrier imb = {};
imb.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
imb.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
imb.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
imb.oldLayout = VK_IMAGE_LAYOUT_GENERAL;
imb.newLayout = VK_IMAGE_LAYOUT_GENERAL;
imb.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
imb.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
imb.image = img->getVkImage();
imb.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
imb.subresourceRange.baseMipLevel = writtenMip;
imb.subresourceRange.levelCount = 1;
imb.subresourceRange.baseArrayLayer = 0;
imb.subresourceRange.layerCount = 1;

vkCmdPipelineBarrier(
*frame.graphicsCommandBuffer,
convertStage(from),
convertStage(to),
0,
0, nullptr,
0, nullptr,
1, &imb
);
}
//else if (from == Stage::Compute && to == Stage::Compute && written != nullptr)
//{
// const Image* img = mandatory_non_null_type_cast< TextureVk* >(written)->getImage();

// VkImageMemoryBarrier imb = {};
// imb.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
// imb.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
// imb.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
// imb.oldLayout = VK_IMAGE_LAYOUT_GENERAL;
// imb.newLayout = VK_IMAGE_LAYOUT_GENERAL;
// imb.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
// imb.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
// imb.image = img->getVkImage();
// imb.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
// imb.subresourceRange.baseMipLevel = writtenMip;
// imb.subresourceRange.levelCount = 1;
// imb.subresourceRange.baseArrayLayer = 0;
// imb.subresourceRange.layerCount = 1;

// vkCmdPipelineBarrier(
// *frame.graphicsCommandBuffer,
// convertStage(from),
// convertStage(to),
// 0,
// 0, nullptr,
// 0, nullptr,
// 1, &imb
// );
//}
else
{
// No memory access; only add an execution barrier.
Expand Down
22 changes: 1 addition & 21 deletions code/World/Deferred/WorldRendererDeferred.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,6 @@ const render::Handle s_persistentVisualTargetSet[] =
render::Handle(L"World_VisualTargetSet_Odd")
};

const render::Handle s_persistentHiZTexture[] =
{
render::Handle(L"World_HiZTexture_0"),
render::Handle(L"World_HiZTexture_1"),
render::Handle(L"World_HiZTexture_2"),
render::Handle(L"World_HiZTexture_3")
};

const resource::Id< render::Shader > c_lightShader(L"{707DE0B0-0E2B-A44A-9441-9B1FCFD428AA}");

}
Expand Down Expand Up @@ -148,19 +140,7 @@ void WorldRendererDeferred::setup(
};

// Add Hi-Z texture.
const Vector2 viewSize = worldRenderView.getViewSize();
const int32_t viewWidth = (int32_t)viewSize.x;
const int32_t viewHeight = (int32_t)viewSize.y;
const int32_t hiZWidth = viewWidth >> 1;
const int32_t hiZHeight = viewHeight >> 1;
const int32_t hiZMipCount = log2(std::max(hiZWidth, hiZHeight)) + 1;

render::RenderGraphTextureDesc rgtxd;
rgtxd.width = hiZWidth;
rgtxd.height = hiZHeight;
rgtxd.mipCount = hiZMipCount;
rgtxd.format = render::TfR32F;
const render::handle_t hizTextureId = renderGraph.addPersistentTexture(L"HiZ", s_persistentHiZTexture[worldRenderView.getIndex()], rgtxd);
const render::handle_t hizTextureId = m_hiZPass->addTexture(worldRenderView, renderGraph);

// Add passes to render graph.
m_lightClusterPass->setup(worldRenderView, m_gatheredView);
Expand Down
4 changes: 1 addition & 3 deletions code/World/Shared/Passes/GBufferPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ render::handle_t GBufferPass::setup(

// Add GBuffer render pass.
Ref< render::RenderPass > rp = new render::RenderPass(L"GBuffer");

// #fixme We cannot add this input since HiZ is created from depth of this pass.
// rp->addInput(hiZTextureId);
rp->addWeakInput(hiZTextureId);

render::Clear clear;
clear.mask = render::CfColor | render::CfDepth | render::CfStencil;
Expand Down
41 changes: 36 additions & 5 deletions code/World/Shared/Passes/HiZPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "Core/Log/Log.h"
#include "Core/Math/Log2.h"
#include "Core/Misc/String.h"
#include "Core/Timer/Profiler.h"
#include "Render/Buffer.h"
Expand All @@ -32,6 +33,14 @@ const resource::Id< render::Shader > c_hiZBuildShader(L"{E8879B75-F646-5D46-8873
const render::Handle s_handleHiZInput(L"World_HiZInput");
const render::Handle s_handleHiZOutput(L"World_HiZOutput");

const render::Handle s_persistentHiZTexture[] =
{
render::Handle(L"World_HiZTexture_0"),
render::Handle(L"World_HiZTexture_1"),
render::Handle(L"World_HiZTexture_2"),
render::Handle(L"World_HiZTexture_3")
};

}

T_IMPLEMENT_RTTI_CLASS(L"traktor.world.HiZPass", HiZPass, Object)
Expand All @@ -46,6 +55,23 @@ bool HiZPass::create(resource::IResourceManager* resourceManager)
return true;
}

render::handle_t HiZPass::addTexture(const WorldRenderView& worldRenderView, render::RenderGraph& renderGraph) const
{
const Vector2 viewSize = worldRenderView.getViewSize();
const int32_t viewWidth = (int32_t)viewSize.x;
const int32_t viewHeight = (int32_t)viewSize.y;
const int32_t hiZWidth = nearestLog2(viewWidth >> 1);
const int32_t hiZHeight = nearestLog2(viewHeight >> 1);
const int32_t hiZMipCount = log2(std::max(hiZWidth, hiZHeight)) + 1;

render::RenderGraphTextureDesc rgtxd;
rgtxd.width = hiZWidth;
rgtxd.height = hiZHeight;
rgtxd.mipCount = hiZMipCount;
rgtxd.format = render::TfR32F;
return renderGraph.addPersistentTexture(L"HiZ", s_persistentHiZTexture[worldRenderView.getIndex()], rgtxd);
}

void HiZPass::setup(
const WorldRenderView& worldRenderView,
render::RenderGraph& renderGraph,
Expand All @@ -56,13 +82,10 @@ void HiZPass::setup(
T_PROFILER_SCOPE(L"HiZPass::setup");

const Vector2 viewSize = worldRenderView.getViewSize();

const int32_t viewWidth = (int32_t)viewSize.x;
const int32_t viewHeight = (int32_t)viewSize.y;

const int32_t hiZWidth = viewWidth >> 1;
const int32_t hiZHeight = viewHeight >> 1;

const int32_t hiZWidth = nearestLog2(viewWidth >> 1);
const int32_t hiZHeight = nearestLog2(viewHeight >> 1);
const int32_t hiZMipCount = log2(std::max(hiZWidth, hiZHeight)) + 1;

Ref< render::RenderPass > rp = new render::RenderPass(L"HiZ");
Expand All @@ -74,6 +97,12 @@ void HiZPass::setup(
const int32_t mipWidth = std::max(hiZWidth >> i, 1);
const int32_t mipHeight = std::max(hiZHeight >> i, 1);

const int32_t workWidth = std::max(viewWidth >> (i + 1), 1);
const int32_t workHeight = std::max(viewHeight >> (i + 1), 1);

T_FATAL_ASSERT(workWidth <= mipWidth);
T_FATAL_ASSERT(workHeight <= mipHeight);

rp->addBuild(
[=](const render::RenderGraph& renderGraph, render::RenderContext* renderContext)
{
Expand All @@ -87,6 +116,8 @@ void HiZPass::setup(

renderBlock->programParams->beginParameters(renderContext);

renderBlock->programParams->setVectorParameter(render::getParameterHandle(L"World_HiZWorkSize"), Vector4(workWidth, workHeight, 0.0f, 0.0f));

const auto outputTexture = renderGraph.getTexture(outputHiZTextureId);
renderBlock->programParams->setImageViewParameter(s_handleHiZOutput, outputTexture, i);

Expand Down
4 changes: 3 additions & 1 deletion code/World/Shared/Passes/HiZPass.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class HiZPass : public Object
public:
bool create(resource::IResourceManager* resourceManager);

render::handle_t addTexture(const WorldRenderView& worldRenderView, render::RenderGraph& renderGraph) const;

void setup(
const WorldRenderView& worldRenderView,
render::RenderGraph& renderGraph,
Expand All @@ -50,7 +52,7 @@ class HiZPass : public Object
) const;

private:
resource::Proxy< render::Shader > m_hiZBuildShader;
resource::Proxy< render::Shader > m_hiZBuildShader;
};

}
Loading

0 comments on commit bf8e450

Please sign in to comment.