-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathConfidencePass.cpp
134 lines (110 loc) · 5.05 KB
/
ConfidencePass.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/***************************************************************************
# Copyright (c) 2020-2023, NVIDIA CORPORATION. All rights reserved.
#
# NVIDIA CORPORATION and its licensors retain all intellectual property
# and proprietary rights in and to this software, related documentation
# and any modifications thereto. Any use, reproduction, disclosure or
# distribution of this software and related documentation without an express
# license agreement from NVIDIA CORPORATION is strictly prohibited.
**************************************************************************/
#include "ConfidencePass.h"
#include "FilterGradientsPass.h"
#include "RenderTargets.h"
#include <donut/engine/ShaderFactory.h>
#include <donut/engine/View.h>
#include <donut/core/log.h>
#include <nvrhi/utils.h>
using namespace donut::math;
#include "../shaders/ShaderParameters.h"
using namespace donut::engine;
ConfidencePass::ConfidencePass(
nvrhi::IDevice* device,
std::shared_ptr<ShaderFactory> shaderFactory)
: m_Device(device)
, m_ShaderFactory(shaderFactory)
{
nvrhi::BindingLayoutDesc bindingLayoutDesc;
bindingLayoutDesc.visibility = nvrhi::ShaderType::Compute;
bindingLayoutDesc.bindings = {
nvrhi::BindingLayoutItem::Texture_SRV(0),
nvrhi::BindingLayoutItem::Texture_SRV(1),
nvrhi::BindingLayoutItem::Texture_SRV(2),
nvrhi::BindingLayoutItem::Texture_SRV(3),
nvrhi::BindingLayoutItem::Texture_UAV(0),
nvrhi::BindingLayoutItem::Texture_UAV(1),
nvrhi::BindingLayoutItem::Sampler(0),
nvrhi::BindingLayoutItem::PushConstants(0, sizeof(ConfidenceConstants))
};
m_BindingLayout = m_Device->createBindingLayout(bindingLayoutDesc);
auto samplerDesc = nvrhi::SamplerDesc()
.setAllFilters(true)
.setAllAddressModes(nvrhi::SamplerAddressMode::ClampToBorder)
.setBorderColor(nvrhi::Color(0.f));
m_Sampler = device->createSampler(samplerDesc);
}
void ConfidencePass::CreatePipeline()
{
donut::log::debug("Initializing ConfidencePass...");
m_ComputeShader = m_ShaderFactory->CreateShader("app/ConfidencePass.hlsl", "main", nullptr, nvrhi::ShaderType::Compute);
nvrhi::ComputePipelineDesc pipelineDesc;
pipelineDesc.bindingLayouts = { m_BindingLayout };
pipelineDesc.CS = m_ComputeShader;
m_ComputePipeline = m_Device->createComputePipeline(pipelineDesc);
}
void ConfidencePass::CreateBindingSet(const RenderTargets& renderTargets)
{
for (int currentFrame = 0; currentFrame <= 1; currentFrame++)
{
nvrhi::BindingSetDesc bindingSetDesc;
bindingSetDesc.bindings = {
nvrhi::BindingSetItem::Texture_SRV(0, renderTargets.Gradients),
nvrhi::BindingSetItem::Texture_SRV(1, renderTargets.MotionVectors),
nvrhi::BindingSetItem::Texture_SRV(2, currentFrame ? renderTargets.PrevDiffuseConfidence : renderTargets.DiffuseConfidence),
nvrhi::BindingSetItem::Texture_SRV(3, currentFrame ? renderTargets.PrevSpecularConfidence : renderTargets.SpecularConfidence),
nvrhi::BindingSetItem::Texture_UAV(0, currentFrame ? renderTargets.DiffuseConfidence : renderTargets.PrevDiffuseConfidence),
nvrhi::BindingSetItem::Texture_UAV(1, currentFrame ? renderTargets.SpecularConfidence : renderTargets.PrevSpecularConfidence),
nvrhi::BindingSetItem::Sampler(0, m_Sampler),
nvrhi::BindingSetItem::PushConstants(0, sizeof(ConfidenceConstants))
};
nvrhi::BindingSetHandle bindingSet = m_Device->createBindingSet(bindingSetDesc, m_BindingLayout);
if (currentFrame)
m_BindingSet = bindingSet;
else
m_PrevBindingSet = bindingSet;
}
m_GradientsTexture = renderTargets.Gradients;
}
void ConfidencePass::Render(
nvrhi::ICommandList* commandList,
const donut::engine::IView& view,
float logDarknessBias,
float sensitivity,
float historyLength,
bool checkerboard)
{
commandList->beginMarker("Confidence");
const auto& gradientsDesc = m_GradientsTexture->getDesc();
ConfidenceConstants constants = {};
constants.viewportSize = dm::uint2(view.GetViewExtent().width(), view.GetViewExtent().height());
constants.invGradientTextureSize.x = 1.f / float(gradientsDesc.width);
constants.invGradientTextureSize.y = 1.f / float(gradientsDesc.height);
constants.darknessBias = ::exp2f(logDarknessBias);
constants.sensitivity = sensitivity;
constants.checkerboard = checkerboard;
constants.blendFactor = 1.f / (historyLength + 1.f);
constants.inputBufferIndex = FilterGradientsPass::GetOutputBufferIndex();
nvrhi::ComputeState state;
state.bindings = { m_BindingSet };
state.pipeline = m_ComputePipeline;
commandList->setComputeState(state);
commandList->setPushConstants(&constants, sizeof(constants));
commandList->dispatch(
dm::div_ceil(view.GetViewExtent().width(), 8),
dm::div_ceil(view.GetViewExtent().height(), 8),
1);
commandList->endMarker();
}
void ConfidencePass::NextFrame()
{
std::swap(m_BindingSet, m_PrevBindingSet);
}