-
Notifications
You must be signed in to change notification settings - Fork 7
/
raytracer.cpp
314 lines (268 loc) · 12.9 KB
/
raytracer.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-FileCopyrightText: Copyright (c) 2019-2021 NVIDIA CORPORATION
* SPDX-License-Identifier: Apache-2.0
*/
#include "raytracer.hpp"
#include "imgui.h"
#include "nvh/fileoperations.hpp"
#include "nvvk/images_vk.hpp"
#include "nvvk/shaders_vk.hpp"
extern std::vector<std::string> defaultSearchPaths;
Raytracer::Raytracer() = default;
//--------------------------------------------------------------------------------------------------
// Initializing the allocator and querying the raytracing properties
//
void Raytracer::setup(const vk::Device& device, const vk::PhysicalDevice& physicalDevice, uint32_t queueIndex, nvvkpp::ResourceAllocator* allocator)
{
m_device = device;
m_queueIndex = queueIndex;
m_debug.setup(device);
m_alloc = allocator;
// Requesting raytracing properties
auto properties = physicalDevice.getProperties2<vk::PhysicalDeviceProperties2, vk::PhysicalDeviceRayTracingPropertiesNV>();
m_rtProperties = properties.get<vk::PhysicalDeviceRayTracingPropertiesNV>();
if(m_rtProperties.shaderGroupHandleSize != 0)
m_bValid = true;
else
{
m_bValid = false;
return;
}
m_rtBuilder.setup(device, allocator, queueIndex);
}
const std::vector<nvvk::Texture>& Raytracer::outputImages() const
{
return m_raytracingOutput;
}
int Raytracer::maxFrames() const
{
return m_maxFrames;
}
void Raytracer::destroy()
{
for(auto& t : m_raytracingOutput)
m_alloc->destroy(t);
m_rtBuilder.destroy();
m_device.destroy(m_descPool);
m_device.destroy(m_descSetLayout);
m_device.destroy(m_pipeline);
m_device.destroy(m_pipelineLayout);
m_alloc->destroy(m_sbtBuffer);
m_alloc->destroy(m_rtPrimLookup);
m_binding.clear();
}
//--------------------------------------------------------------------------------------------------
// Making all output images: color, normal, ...
//
void Raytracer::createOutputImages(vk::Extent2D size)
{
for(auto& t : m_raytracingOutput)
m_alloc->destroy(t);
m_raytracingOutput.clear();
m_outputSize = size;
auto usage = vk::ImageUsageFlagBits::eSampled | vk::ImageUsageFlagBits::eStorage | vk::ImageUsageFlagBits::eTransferSrc;
vk::DeviceSize imgSize = size.width * size.height * 4 * sizeof(float);
vk::Format format = vk::Format::eR32G32B32A32Sfloat;
// Create two output image, the color and the data
for(int i = 0; i < 2; i++)
{
nvvkpp::ScopeCommandBuffer cmdBuf(m_device, m_queueIndex);
vk::SamplerCreateInfo samplerCreateInfo; // default values
vk::ImageCreateInfo imageCreateInfo = nvvkpp::makeImage2DCreateInfo(size, format, usage);
nvvk::Image image = m_alloc->createImage(cmdBuf, imgSize, nullptr, imageCreateInfo, vk::ImageLayout::eGeneral);
vk::ImageViewCreateInfo ivInfo = nvvkpp::makeImageViewCreateInfo(image.image, imageCreateInfo);
nvvk::Texture txt = m_alloc->createTexture(image, ivInfo, samplerCreateInfo);
txt.descriptor.imageLayout = VK_IMAGE_LAYOUT_GENERAL;
m_raytracingOutput.push_back(txt);
}
m_alloc->finalizeAndReleaseStaging();
}
void Raytracer::createDescriptorSet()
{
using vkDS = vk::DescriptorSetLayoutBinding;
using vkDT = vk::DescriptorType;
using vkSS = vk::ShaderStageFlagBits;
uint32_t nbOutput = static_cast<uint32_t>(m_raytracingOutput.size());
m_binding.addBinding(vkDS(0, vkDT::eAccelerationStructureNV, 1, vkSS::eRaygenNV | vkSS::eClosestHitNV));
m_binding.addBinding(vkDS(1, vkDT::eStorageImage, nbOutput, vkSS::eRaygenNV)); // Output image
m_binding.addBinding(vkDS(2, vkDT::eStorageBuffer, 1, vkSS::eClosestHitNV | vkSS::eAnyHitNV)); // Primitive info
m_descPool = m_binding.createPool(m_device);
m_descSetLayout = m_binding.createLayout(m_device);
m_descSet = m_device.allocateDescriptorSets({m_descPool, 1, &m_descSetLayout})[0];
std::vector<vk::WriteDescriptorSet> writes;
vk::AccelerationStructureNV tlas = m_rtBuilder.getAccelerationStructure();
vk::WriteDescriptorSetAccelerationStructureNV descAsInfo{1, &tlas};
vk::DescriptorBufferInfo primitiveInfoDesc{m_rtPrimLookup.buffer, 0, VK_WHOLE_SIZE};
writes.emplace_back(m_binding.makeWrite(m_descSet, 0, &descAsInfo));
std::vector<vk::DescriptorImageInfo> descImgInfo;
for(auto& i : m_raytracingOutput)
{
descImgInfo.push_back(i.descriptor);
}
writes.emplace_back(m_binding.makeWriteArray(m_descSet, 1, descImgInfo.data()));
writes.emplace_back(m_binding.makeWrite(m_descSet, 2, &primitiveInfoDesc));
m_device.updateDescriptorSets(static_cast<uint32_t>(writes.size()), writes.data(), 0, nullptr);
updateDescriptorSet();
}
void Raytracer::updateDescriptorSet()
{
// (1) Output buffer
{
std::vector<vk::DescriptorImageInfo> descImgInfo;
for(auto& i : m_raytracingOutput)
{
descImgInfo.push_back(i.descriptor);
}
vk::WriteDescriptorSet wds = m_binding.makeWriteArray(m_descSet, 1, descImgInfo.data());
//vk::DescriptorImageInfo imageInfo{{}, m_raytracingOutput.descriptor.imageView, vk::ImageLayout::eGeneral};
//vk::WriteDescriptorSet wds{m_rtDescSet, 1, 0, 1, vkDT::eStorageImage, &imageInfo};
m_device.updateDescriptorSets(wds, nullptr);
}
}
void Raytracer::createPipeline(const vk::DescriptorSetLayout& sceneDescSetLayout)
{
vk::ShaderModule raygenSM =
nvvk::createShaderModule(m_device, nvh::loadFile("spv/raytrace.rgen.spv", true, defaultSearchPaths));
vk::ShaderModule missSM = nvvk::createShaderModule(m_device, nvh::loadFile("spv/raytrace.rmiss.spv", true, defaultSearchPaths));
vk::ShaderModule shadowmissSM =
nvvk::createShaderModule(m_device, nvh::loadFile("spv/raytraceShadow.rmiss.spv", true, defaultSearchPaths));
vk::ShaderModule chitSM = nvvk::createShaderModule(m_device, nvh::loadFile("spv/raytrace.rchit.spv", true, defaultSearchPaths));
std::vector<vk::PipelineShaderStageCreateInfo> stages;
// Raygen
vk::RayTracingShaderGroupCreateInfoNV rg{vk::RayTracingShaderGroupTypeNV::eGeneral, VK_SHADER_UNUSED_NV,
VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV};
stages.push_back({{}, vk::ShaderStageFlagBits::eRaygenNV, raygenSM, "main"});
rg.setGeneralShader(static_cast<uint32_t>(stages.size() - 1));
m_groups.push_back(rg);
// Miss
vk::RayTracingShaderGroupCreateInfoNV mg{vk::RayTracingShaderGroupTypeNV::eGeneral, VK_SHADER_UNUSED_NV,
VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV};
stages.push_back({{}, vk::ShaderStageFlagBits::eMissNV, missSM, "main"});
mg.setGeneralShader(static_cast<uint32_t>(stages.size() - 1));
m_groups.push_back(mg);
// Shadow Miss
stages.push_back({{}, vk::ShaderStageFlagBits::eMissNV, shadowmissSM, "main"});
mg.setGeneralShader(static_cast<uint32_t>(stages.size() - 1));
m_groups.push_back(mg);
// Hit Group - Closest Hit + AnyHit
vk::RayTracingShaderGroupCreateInfoNV hg{vk::RayTracingShaderGroupTypeNV::eTrianglesHitGroup, VK_SHADER_UNUSED_NV,
VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV};
stages.push_back({{}, vk::ShaderStageFlagBits::eClosestHitNV, chitSM, "main"});
hg.setClosestHitShader(static_cast<uint32_t>(stages.size() - 1));
m_groups.push_back(hg);
// Push constant: ray depth, ...
vk::PushConstantRange pushConstant{vk::ShaderStageFlagBits::eRaygenNV | vk::ShaderStageFlagBits::eClosestHitNV
| vk::ShaderStageFlagBits::eMissNV,
0, sizeof(PushConstant)};
// All 3 descriptors
std::vector<vk::DescriptorSetLayout> allLayouts = {m_descSetLayout, sceneDescSetLayout};
vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo;
pipelineLayoutCreateInfo.setSetLayoutCount(static_cast<uint32_t>(allLayouts.size()));
pipelineLayoutCreateInfo.setPSetLayouts(allLayouts.data());
pipelineLayoutCreateInfo.setPushConstantRangeCount(1);
pipelineLayoutCreateInfo.setPPushConstantRanges(&pushConstant);
m_pipelineLayout = m_device.createPipelineLayout(pipelineLayoutCreateInfo);
m_debug.setObjectName(m_pipelineLayout, "raytracer");
// Assemble the shader stages and recursion depth info into the raytracing pipeline
vk::RayTracingPipelineCreateInfoNV rayPipelineInfo;
rayPipelineInfo.setStageCount(static_cast<uint32_t>(stages.size()));
rayPipelineInfo.setPStages(stages.data());
rayPipelineInfo.setGroupCount(static_cast<uint32_t>(m_groups.size()));
rayPipelineInfo.setPGroups(m_groups.data());
rayPipelineInfo.setMaxRecursionDepth(10);
rayPipelineInfo.setLayout(m_pipelineLayout);
m_pipeline = m_device.createRayTracingPipelineNV({}, rayPipelineInfo).value;
m_device.destroyShaderModule(raygenSM);
m_device.destroyShaderModule(missSM);
m_device.destroyShaderModule(shadowmissSM);
m_device.destroyShaderModule(chitSM);
}
//--------------------------------------------------------------------------------------------------
//
//
void Raytracer::createShadingBindingTable()
{
auto groupCount = static_cast<uint32_t>(m_groups.size()); // 3 shaders: raygen, miss, chit
uint32_t groupHandleSize = m_rtProperties.shaderGroupHandleSize; // Size of a program identifier
uint32_t baseAlignment = m_rtProperties.shaderGroupBaseAlignment; // Size of a program identifier
// Fetch all the shader handles used in the pipeline, so that they can be written in the SBT
uint32_t sbtSize = groupCount * baseAlignment;
std::vector<uint8_t> shaderHandleStorage(sbtSize);
auto result = m_device.getRayTracingShaderGroupHandlesNV(m_pipeline, 0, groupCount, sbtSize, shaderHandleStorage.data());
assert(result == vk::Result::eSuccess);
m_sbtBuffer = m_alloc->createBuffer(sbtSize, vk::BufferUsageFlagBits::eTransferSrc,
vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent);
m_debug.setObjectName(m_sbtBuffer.buffer, std::string("SBT").c_str());
// Write the handles in the SBT
void* mapped = m_alloc->map(m_sbtBuffer);
auto* pData = reinterpret_cast<uint8_t*>(mapped);
for(uint32_t g = 0; g < groupCount; g++)
{
memcpy(pData, shaderHandleStorage.data() + g * groupHandleSize, groupHandleSize); // raygen
pData += baseAlignment;
}
m_alloc->unmap(m_sbtBuffer);
}
//--------------------------------------------------------------------------------------------------
//
//
void Raytracer::run(const vk::CommandBuffer& cmdBuf, const vk::DescriptorSet& sceneDescSet, int frame /*= 0*/)
{
m_pushC.frame = frame;
uint32_t progSize = m_rtProperties.shaderGroupBaseAlignment; // Size of a program identifier
cmdBuf.bindPipeline(vk::PipelineBindPoint::eRayTracingNV, m_pipeline);
cmdBuf.bindDescriptorSets(vk::PipelineBindPoint::eRayTracingNV, m_pipelineLayout, 0, {m_descSet, sceneDescSet}, {});
cmdBuf.pushConstants<PushConstant>(m_pipelineLayout,
vk::ShaderStageFlagBits::eRaygenNV | vk::ShaderStageFlagBits::eClosestHitNV
| vk::ShaderStageFlagBits::eMissNV,
0, m_pushC);
vk::DeviceSize rayGenOffset = 0 * progSize;
vk::DeviceSize missOffset = 1 * progSize;
vk::DeviceSize missStride = progSize;
vk::DeviceSize hitGroupOffset = 3 * progSize; // Jump over the 2 miss
vk::DeviceSize hitGroupStride = progSize;
cmdBuf.traceRaysNV(m_sbtBuffer.buffer, rayGenOffset, //
m_sbtBuffer.buffer, missOffset, missStride, //
m_sbtBuffer.buffer, hitGroupOffset, hitGroupStride, //
m_sbtBuffer.buffer, 0, 0, //
m_outputSize.width, m_outputSize.height, //
1 /*, NVVKPP_DISPATCHER*/);
}
bool Raytracer::uiSetup()
{
bool modified = false;
if(ImGui::CollapsingHeader("Ray Tracing"))
{
modified = false;
modified |= ImGui::SliderFloat("Max Ray Length", &m_pushC.maxRayLenght, 1, 1000000, "%.1f");
modified |= ImGui::SliderInt("Samples Per Frame", &m_pushC.samples, 1, 100);
modified |= ImGui::SliderInt("Max Iteration ", &m_maxFrames, 1, 1000);
}
return modified;
}
void Raytracer::setClearColor(glm::vec3& _color)
{
m_pushC.backgroundColor = _color;
}
void Raytracer::setToonSteps(int nbStep)
{
m_pushC.nbSteps = nbStep;
}
void Raytracer::setToonLightDir(glm::vec3 lightDir)
{
m_pushC.lightDir = lightDir;
}