|
| 1 | +/* |
| 2 | + * Copyright 2025 Diligent Graphics LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * In no event and under no legal theory, whether in tort (including negligence), |
| 17 | + * contract, or otherwise, unless required by applicable law (such as deliberate |
| 18 | + * and grossly negligent acts) or agreed to in writing, shall any Contributor be |
| 19 | + * liable for any damages, including any direct, indirect, special, incidental, |
| 20 | + * or consequential damages of any character arising as a result of this License or |
| 21 | + * out of the use or inability to use the software (including but not limited to damages |
| 22 | + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and |
| 23 | + * all other commercial damages or losses), even if such Contributor has been advised |
| 24 | + * of the possibility of such damages. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "Tasks/HnComputeDepthBoundsTask.hpp" |
| 28 | +#include "HnRenderDelegate.hpp" |
| 29 | +#include "ScopedDebugGroup.hpp" |
| 30 | +#include "HnRenderParam.hpp" |
| 31 | +#include "DepthRangeCalculator.hpp" |
| 32 | +#include "HnFrameRenderTargets.hpp" |
| 33 | +#include "HnTokens.hpp" |
| 34 | + |
| 35 | +namespace Diligent |
| 36 | +{ |
| 37 | + |
| 38 | +namespace HLSL |
| 39 | +{ |
| 40 | +#include "Shaders/Common/public/BasicStructures.fxh" |
| 41 | +#include "Shaders/Common/private/ComputeDepthRangeStructs.fxh" |
| 42 | +} // namespace HLSL |
| 43 | + |
| 44 | +namespace USD |
| 45 | +{ |
| 46 | + |
| 47 | +HnComputeDepthBoundsTask::HnComputeDepthBoundsTask(pxr::HdSceneDelegate* ParamsDelegate, const pxr::SdfPath& Id) : |
| 48 | + HnTask{Id} |
| 49 | +{ |
| 50 | +} |
| 51 | + |
| 52 | +HnComputeDepthBoundsTask::~HnComputeDepthBoundsTask() |
| 53 | +{ |
| 54 | +} |
| 55 | + |
| 56 | +void HnComputeDepthBoundsTask::Sync(pxr::HdSceneDelegate* Delegate, |
| 57 | + pxr::HdTaskContext* TaskCtx, |
| 58 | + pxr::HdDirtyBits* DirtyBits) |
| 59 | +{ |
| 60 | + *DirtyBits = pxr::HdChangeTracker::Clean; |
| 61 | +} |
| 62 | + |
| 63 | +bool HnComputeDepthBoundsTask::IsActive(pxr::HdRenderIndex& RenderIndex) const |
| 64 | +{ |
| 65 | + pxr::HdRenderDelegate* RenderDelegate = RenderIndex.GetRenderDelegate(); |
| 66 | + const HnRenderParam* RenderParam = static_cast<const HnRenderParam*>(RenderDelegate->GetRenderParam()); |
| 67 | + const HN_RENDER_MODE RenderMode = RenderParam->GetRenderMode(); |
| 68 | + const PBR_Renderer::DebugViewType DebugView = RenderParam->GetDebugView(); |
| 69 | + |
| 70 | + // Only run this task when scene depth debug view is enabled |
| 71 | + return RenderMode == HN_RENDER_MODE_SOLID && DebugView == PBR_Renderer::DebugViewType::SceneDepth; |
| 72 | +} |
| 73 | + |
| 74 | +void HnComputeDepthBoundsTask::Prepare(pxr::HdTaskContext* TaskCtx, |
| 75 | + pxr::HdRenderIndex* RenderIndex) |
| 76 | +{ |
| 77 | + m_RenderIndex = RenderIndex; |
| 78 | + |
| 79 | + m_FrameTargets = GetFrameRenderTargets(TaskCtx); |
| 80 | + if (m_FrameTargets == nullptr) |
| 81 | + { |
| 82 | + UNEXPECTED("Framebuffer targets are null"); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + if (m_FrameTargets->Version != m_FrameRenderTargetsVersion) |
| 87 | + { |
| 88 | + m_FrameRenderTargetsVersion = m_FrameTargets->Version; |
| 89 | + m_ComputeDepthRangeSRBs = {}; |
| 90 | + } |
| 91 | + |
| 92 | + HnRenderDelegate* RenderDelegate = static_cast<HnRenderDelegate*>(RenderIndex->GetRenderDelegate()); |
| 93 | + const HnRenderParam* RenderParam = static_cast<const HnRenderParam*>(RenderDelegate->GetRenderParam()); |
| 94 | + |
| 95 | + if (!m_DepthRangeCalculator) |
| 96 | + { |
| 97 | + DepthRangeCalculator::CreateInfo DepthRangeCalcCI; |
| 98 | + DepthRangeCalcCI.pDevice = RenderDelegate->GetDevice(); |
| 99 | + DepthRangeCalcCI.pStateCache = RenderDelegate->GetRenderStateCache(); |
| 100 | + DepthRangeCalcCI.PackMatrixRowMajor = RenderDelegate->GetUSDRenderer()->GetSettings().PackMatrixRowMajor; |
| 101 | + DepthRangeCalcCI.AsyncShaders = RenderParam->GetConfig().AsyncShaderCompilation; |
| 102 | + |
| 103 | + try |
| 104 | + { |
| 105 | + m_DepthRangeCalculator = std::make_unique<DepthRangeCalculator>(DepthRangeCalcCI); |
| 106 | + } |
| 107 | + catch (const std::exception& e) |
| 108 | + { |
| 109 | + UNEXPECTED("Failed to create DepthRangeCalculator: ", e.what()); |
| 110 | + } |
| 111 | + catch (...) |
| 112 | + { |
| 113 | + UNEXPECTED("Failed to create DepthRangeCalculator"); |
| 114 | + } |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +void HnComputeDepthBoundsTask::Execute(pxr::HdTaskContext* TaskCtx) |
| 119 | +{ |
| 120 | + if (m_RenderIndex == nullptr) |
| 121 | + { |
| 122 | + UNEXPECTED("Render index is null. This likely indicates that Prepare() has not been called."); |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + if (!m_DepthRangeCalculator) |
| 127 | + { |
| 128 | + UNEXPECTED("DepthRangeCalculator is null. This likely indicates that Prepare() has not been called."); |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + if (!m_DepthRangeCalculator->IsReady()) |
| 133 | + { |
| 134 | + return; |
| 135 | + } |
| 136 | + |
| 137 | + ITexture* pDepth = m_FrameTargets->DepthDSV != nullptr ? m_FrameTargets->DepthDSV->GetTexture() : nullptr; |
| 138 | + if (pDepth == nullptr) |
| 139 | + { |
| 140 | + UNEXPECTED("Depth stencil view is null"); |
| 141 | + return; |
| 142 | + } |
| 143 | + |
| 144 | + ITextureView* pDepthSRV = pDepth->GetDefaultView(TEXTURE_VIEW_SHADER_RESOURCE); |
| 145 | + if (pDepthSRV == nullptr) |
| 146 | + { |
| 147 | + UNEXPECTED("Depth SRV is null"); |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + HnRenderDelegate* RenderDelegate = static_cast<HnRenderDelegate*>(m_RenderIndex->GetRenderDelegate()); |
| 152 | + const HnRenderParam* RenderParam = static_cast<const HnRenderParam*>(RenderDelegate->GetRenderParam()); |
| 153 | + IDeviceContext* pCtx = RenderDelegate->GetDeviceContext(); |
| 154 | + IBuffer* pFrameAttribsCB = RenderDelegate->GetFrameAttribsCB(); |
| 155 | + |
| 156 | + RefCntAutoPtr<IShaderResourceBinding>& ComputeDepthRangeSRB = m_ComputeDepthRangeSRBs[RenderParam->GetFrameNumber() & 0x01]; |
| 157 | + if (!ComputeDepthRangeSRB) |
| 158 | + { |
| 159 | + ComputeDepthRangeSRB = m_DepthRangeCalculator->CreateSRB(pDepthSRV, pFrameAttribsCB); |
| 160 | + } |
| 161 | + |
| 162 | + ScopedDebugGroup DebugGroup{pCtx, "Compute Depth Bounds"}; |
| 163 | + |
| 164 | + const TextureDesc& DepthDesc = pDepth->GetDesc(); |
| 165 | + |
| 166 | + DepthRangeCalculator::ComputeRangeAttribs Attribs; |
| 167 | + Attribs.pContext = pCtx; |
| 168 | + Attribs.pSRB = ComputeDepthRangeSRB; |
| 169 | + Attribs.Width = DepthDesc.Width; |
| 170 | + Attribs.Height = DepthDesc.Height; |
| 171 | + m_DepthRangeCalculator->ComputeRange(Attribs); |
| 172 | + |
| 173 | + // Copy the depth range to the frame attributes constant buffer |
| 174 | + if (IBuffer* pDepthRangeBuffer = m_DepthRangeCalculator->GetDepthRangeBuffer()) |
| 175 | + { |
| 176 | + pCtx->CopyBuffer(pDepthRangeBuffer, 0, RESOURCE_STATE_TRANSITION_MODE_TRANSITION, |
| 177 | + pFrameAttribsCB, offsetof(HLSL::CameraAttribs, fSceneNearZ), sizeof(HLSL::DepthRangeI), RESOURCE_STATE_TRANSITION_MODE_TRANSITION); |
| 178 | + |
| 179 | + StateTransitionDesc Barrier{pFrameAttribsCB, RESOURCE_STATE_UNKNOWN, RESOURCE_STATE_CONSTANT_BUFFER, STATE_TRANSITION_FLAG_UPDATE_STATE}; |
| 180 | + pCtx->TransitionResourceStates(1, &Barrier); |
| 181 | + } |
| 182 | + else |
| 183 | + { |
| 184 | + UNEXPECTED("Depth range buffer is null"); |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +} // namespace USD |
| 189 | + |
| 190 | +} // namespace Diligent |
0 commit comments