-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmeshlets.cpp
152 lines (122 loc) · 5.02 KB
/
meshlets.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Copyright (c) 2014-2021, 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.
*/
#include <donut/app/ApplicationBase.h>
#include <donut/engine/ShaderFactory.h>
#include <donut/app/DeviceManager.h>
#include <donut/core/log.h>
#include <donut/core/vfs/VFS.h>
#include <nvrhi/utils.h>
using namespace donut;
static const char* g_WindowTitle = "Donut Example: Meshlets";
class MeshletExample : public app::IRenderPass
{
private:
nvrhi::ShaderHandle m_AmplificationShader;
nvrhi::ShaderHandle m_MeshShader;
nvrhi::ShaderHandle m_PixelShader;
nvrhi::MeshletPipelineHandle m_Pipeline;
nvrhi::CommandListHandle m_CommandList;
public:
using IRenderPass::IRenderPass;
bool Init()
{
auto nativeFS = std::make_shared<vfs::NativeFileSystem>();
std::filesystem::path appShaderPath = app::GetDirectoryWithExecutable() / "shaders/meshlets" / app::GetShaderTypeName(GetDevice()->getGraphicsAPI());
engine::ShaderFactory shaderFactory(GetDevice(), nativeFS, appShaderPath);
m_AmplificationShader = shaderFactory.CreateShader("shaders.hlsl", "main_as", nullptr, nvrhi::ShaderType::Amplification);
m_MeshShader = shaderFactory.CreateShader("shaders.hlsl", "main_ms", nullptr, nvrhi::ShaderType::Mesh);
m_PixelShader = shaderFactory.CreateShader("shaders.hlsl", "main_ps", nullptr, nvrhi::ShaderType::Pixel);
if (!m_AmplificationShader || !m_MeshShader || !m_PixelShader)
{
return false;
}
m_CommandList = GetDevice()->createCommandList();
return true;
}
void Animate(float fElapsedTimeSeconds) override
{
GetDeviceManager()->SetInformativeWindowTitle(g_WindowTitle);
}
void BackBufferResizing() override
{
m_Pipeline = nullptr;
}
void Render(nvrhi::IFramebuffer* framebuffer) override
{
if (!m_Pipeline)
{
nvrhi::MeshletPipelineDesc psoDesc;
psoDesc.AS = m_AmplificationShader;
psoDesc.MS = m_MeshShader;
psoDesc.PS = m_PixelShader;
psoDesc.primType = nvrhi::PrimitiveType::TriangleList;
psoDesc.renderState.depthStencilState.depthTestEnable = false;
m_Pipeline = GetDevice()->createMeshletPipeline(psoDesc, framebuffer);
}
m_CommandList->open();
nvrhi::utils::ClearColorAttachment(m_CommandList, framebuffer, 0, nvrhi::Color(0.f));
nvrhi::MeshletState state;
state.pipeline = m_Pipeline;
state.framebuffer = framebuffer;
state.viewport.addViewportAndScissorRect(framebuffer->getFramebufferInfo().getViewport());
m_CommandList->setMeshletState(state);
m_CommandList->dispatchMesh(1);
m_CommandList->close();
GetDevice()->executeCommandList(m_CommandList);
}
};
#ifdef WIN32
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
#else
int main(int __argc, const char** __argv)
#endif
{
nvrhi::GraphicsAPI api = app::GetGraphicsAPIFromCommandLine(__argc, __argv);
app::DeviceManager* deviceManager = app::DeviceManager::Create(api);
app::DeviceCreationParameters deviceParams;
#ifdef _DEBUG
deviceParams.enableDebugRuntime = true;
deviceParams.enableNvrhiValidationLayer = true;
#endif
if (!deviceManager->CreateWindowDeviceAndSwapChain(deviceParams, g_WindowTitle))
{
log::fatal("Cannot initialize a graphics device with the requested parameters");
return 1;
}
if (!deviceManager->GetDevice()->queryFeatureSupport(nvrhi::Feature::Meshlets))
{
log::fatal("The graphics device does not support Meshlets");
return 1;
}
{
MeshletExample example(deviceManager);
if (example.Init())
{
deviceManager->AddRenderPassToBack(&example);
deviceManager->RunMessageLoop();
deviceManager->RemoveRenderPass(&example);
}
}
deviceManager->Shutdown();
delete deviceManager;
return 0;
}