-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathDepthPass.h
145 lines (122 loc) · 5.75 KB
/
DepthPass.h
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
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright (c) 2014-2024, NVIDIA CORPORATION. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include <donut/engine/SceneTypes.h>
#include <donut/render/GeometryPasses.h>
#include <memory>
#include <mutex>
#include <nvrhi/nvrhi.h>
namespace donut::engine
{
class ShaderFactory;
class CommonRenderPasses;
class FramebufferFactory;
class MaterialBindingCache;
class ICompositeView;
class IView;
}
namespace donut::render
{
class DepthPass : public IGeometryPass
{
public:
union PipelineKey
{
struct
{
nvrhi::RasterCullMode cullMode : 2;
bool alphaTested : 1;
bool frontCounterClockwise : 1;
bool reverseDepth : 1;
} bits;
uint32_t value;
static constexpr size_t Count = 1 << 5;
};
class Context : public GeometryPassContext
{
public:
nvrhi::BindingSetHandle inputBindingSet;
PipelineKey keyTemplate;
uint32_t positionOffset = 0;
uint32_t texCoordOffset = 0;
Context()
{
keyTemplate.value = 0;
}
};
struct CreateParameters
{
std::shared_ptr<engine::MaterialBindingCache> materialBindings;
int depthBias = 0;
float depthBiasClamp = 0.f;
float slopeScaledDepthBias = 0.f;
bool trackLiveness = true;
// Switches between loading vertex data through the Input Assembler (true) or buffer SRVs (false).
// Using Buffer SRVs is often faster.
bool useInputAssembler = false;
uint32_t numConstantBufferVersions = 16;
};
protected:
nvrhi::DeviceHandle m_Device;
nvrhi::InputLayoutHandle m_InputLayout;
nvrhi::ShaderHandle m_VertexShader;
nvrhi::ShaderHandle m_PixelShader;
nvrhi::BindingLayoutHandle m_InputBindingLayout;
nvrhi::BindingLayoutHandle m_ViewBindingLayout;
nvrhi::BufferHandle m_DepthCB;
nvrhi::BindingSetHandle m_ViewBindingSet;
nvrhi::GraphicsPipelineHandle m_Pipelines[PipelineKey::Count];
std::mutex m_Mutex;
int m_DepthBias = 0;
float m_DepthBiasClamp = 0.f;
float m_SlopeScaledDepthBias = 0.f;
bool m_IsDX11 = false;
bool m_UseInputAssembler = false;
bool m_TrackLiveness = true;
std::unordered_map<const engine::BufferGroup*, nvrhi::BindingSetHandle> m_InputBindingSets;
std::shared_ptr<engine::CommonRenderPasses> m_CommonPasses;
std::shared_ptr<engine::MaterialBindingCache> m_MaterialBindings;
virtual nvrhi::ShaderHandle CreateVertexShader(engine::ShaderFactory& shaderFactory, const CreateParameters& params);
virtual nvrhi::ShaderHandle CreatePixelShader(engine::ShaderFactory& shaderFactory, const CreateParameters& params);
virtual nvrhi::InputLayoutHandle CreateInputLayout(nvrhi::IShader* vertexShader, const CreateParameters& params);
virtual nvrhi::BindingLayoutHandle CreateInputBindingLayout();
virtual nvrhi::BindingSetHandle CreateInputBindingSet(const engine::BufferGroup* bufferGroup);
virtual void CreateViewBindings(nvrhi::BindingLayoutHandle& layout, nvrhi::BindingSetHandle& set, const CreateParameters& params);
virtual std::shared_ptr<engine::MaterialBindingCache> CreateMaterialBindingCache(engine::CommonRenderPasses& commonPasses);
virtual nvrhi::GraphicsPipelineHandle CreateGraphicsPipeline(PipelineKey key, nvrhi::IFramebuffer* framebuffer);
nvrhi::BindingSetHandle GetOrCreateInputBindingSet(const engine::BufferGroup* bufferGroup);
public:
DepthPass(
nvrhi::IDevice* device,
std::shared_ptr<engine::CommonRenderPasses> commonPasses);
virtual void Init(
engine::ShaderFactory& shaderFactory,
const CreateParameters& params);
void ResetBindingCache();
// IGeometryPass implementation
[[nodiscard]] engine::ViewType::Enum GetSupportedViewTypes() const override;
void SetupView(GeometryPassContext& context, nvrhi::ICommandList* commandList, const engine::IView* view, const engine::IView* viewPrev) override;
bool SetupMaterial(GeometryPassContext& context, const engine::Material* material, nvrhi::RasterCullMode cullMode, nvrhi::GraphicsState& state) override;
void SetupInputBuffers(GeometryPassContext& context, const engine::BufferGroup* buffers, nvrhi::GraphicsState& state) override;
void SetPushConstants(GeometryPassContext& context, nvrhi::ICommandList* commandList, nvrhi::GraphicsState& state, nvrhi::DrawArguments& args) override;
};
}