Skip to content

Commit

Permalink
final presentation code release
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhihao Ruan committed Dec 14, 2021
1 parent 5ca4530 commit 3f90de4
Show file tree
Hide file tree
Showing 20 changed files with 897 additions and 311 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if(UNIX AND NOT APPLE)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "/usr/local/lib/cmake/OpenVDB")
endif(UNIX AND NOT APPLE)

project(volume_restir)
project(vk_raytracing_vdb)

#--------------------------------------------------------------------------------------------------
# look for nvpro_core 1) as a sub-folder 2) at some other locations
Expand Down
419 changes: 360 additions & 59 deletions src/Renderer.cpp

Large diffs are not rendered by default.

43 changes: 41 additions & 2 deletions src/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ class Renderer : public nvvk::AppBaseVk {
void updateGBufferFrameIdx();
uint32_t getCurrentFrameIdx() { return m_currentGBufferFrameIdx; }

// Compute shaders
void createCompDescriptors();
void updateCompDescriptors(nvvk::Buffer& a_spheresAabbBuffer,
nvvk::Buffer& a_spheres);
void createCompPipelines();
void animationObject(float time);

// getters and setters
std::vector<Sphere>& getSpheres() { return m_spheres; }
PushConstantRaster& getPushConstant() { return m_pcRaster; }
Expand All @@ -147,6 +154,15 @@ class Renderer : public nvvk::AppBaseVk {
return m_restirDescSets[getCurrentFrameIdx()];
}

VkPipelineLayout getRestirPostPipelineLayout() {
return m_restirPostPipelineLayout;
}

PushConstantRestir* getRestirPostPipelinePC() { return &m_pcRestirPost; }

void updateFrame();
void resetFrame();

private:
// Information pushed at each draw call
PushConstantRaster m_pcRaster{
Expand All @@ -156,6 +172,7 @@ class Renderer : public nvvk::AppBaseVk {
1000.f, // light intensity
0 // light type
};
float t0 = 0.f; // old time

// Array of objects and instances in the scene
std::vector<ObjModel> m_objModel; // Model on host
Expand All @@ -166,6 +183,12 @@ class Renderer : public nvvk::AppBaseVk {
VkPipelineLayout m_pipelineLayout;
VkPipeline m_graphicsPipeline;

// Acceleration structures
std::vector<VkAccelerationStructureInstanceKHR> tlas;
std::vector<nvvk::RaytracingBuilderKHR::BlasInput> allBlas;

int SphereBlasID;

nvvk::Buffer m_bGlobals; // Device-Host of the camera matrices
nvvk::Buffer m_bObjDesc; // Device buffer of the OBJ descriptions

Expand All @@ -191,7 +214,7 @@ class Renderer : public nvvk::AppBaseVk {
VkPipelineLayout m_postPipelineLayout{VK_NULL_HANDLE};
VkRenderPass m_offscreenRenderPass{VK_NULL_HANDLE};
VkFramebuffer m_offscreenFramebuffer{VK_NULL_HANDLE};
nvvk::Texture m_offscreenColor; // FIXME: output img buffer from RtPipeline
nvvk::Texture m_offscreenColor; // output img buffer from RtPipeline
nvvk::Texture m_offscreenDepth;
VkFormat m_offscreenColorFormat{VK_FORMAT_R32G32B32A32_SFLOAT};
VkFormat m_offscreenDepthFormat{VK_FORMAT_X8_D24_UNORM_PACK32};
Expand Down Expand Up @@ -229,7 +252,7 @@ class Renderer : public nvvk::AppBaseVk {
std::vector<nvvk::Texture> m_reservoirWeightBuffers;
nvvk::Texture m_reservoirTmpInfoBuffer;
nvvk::Texture m_reservoirTmpWeightBuffer;
// FIXME: output img buffer from `Restir`Pipeline
// TODO: output img buffer from `Restir`Pipeline
// may have to combine it with m_offscreenColor
nvvk::Texture m_storageImage;

Expand Down Expand Up @@ -282,13 +305,29 @@ class Renderer : public nvvk::AppBaseVk {
// Push constant for ray tracer
PushConstantRay m_pcRay{};

// Push constant for restir post pipeline
PushConstantRestir m_pcRestirPost{0.f, 0.f, 0.f, 0, 1};

std::vector<Sphere> m_spheres; // All spheres
nvvk::Buffer m_spheresBuffer; // Buffer holding the spheres
nvvk::Buffer m_spheresAabbBuffer; // Buffer of all Aabb
nvvk::Buffer m_spheresMatColorBuffer; // Multiple materials
nvvk::Buffer
m_spheresMatIndexBuffer; // Define which sphere uses which material

//#VKCompute
std::vector<Velocity> m_spheresVelocity; // All spheres
nvvk::Buffer m_spheresVelocityBuffer;
nvvk::DescriptorSetBindings m_compDescSetLayoutBind;
VkDescriptorPool m_compDescPool;
VkDescriptorSetLayout m_compDescSetLayout;
VkDescriptorSet m_compDescSet;
VkPipeline m_compPipeline;
VkPipelineLayout m_compPipelineLayout;

nvvk::RaytracingBuilderKHR::BlasInput gltfToGeometryKHR(
const VkDevice& device, const nvh::GltfPrimMesh& prim);

std::vector<GltfMaterials> m_sphereMaterials;
nvvk::Buffer m_sphereMaterialsBuffer;
};
1 change: 1 addition & 0 deletions src/Vertex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

#include <nvmath/nvmath.h>
#include <vulkan/vulkan.h>

#include <array>

namespace volume_restir {
Expand Down
8 changes: 4 additions & 4 deletions src/common/gltf_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ void GLTFLoader::loadScene(const std::string filename) {
ImGuiH::SetCameraJsonFile(fs::path(filename).stem().string());
if (!m_gltfScene.m_cameras.empty()) {
auto& c = m_gltfScene.m_cameras[0];
CameraManip.setCamera(
{c.eye, c.center, c.up, (float)rad2deg(c.cam.perspective.yfov)});
//CameraManip.setCamera(
// {c.eye, c.center, c.up, (float)rad2deg(c.cam.perspective.yfov)});
ImGuiH::SetHomeCamera(
{c.eye, c.center, c.up, (float)rad2deg(c.cam.perspective.yfov)});

Expand All @@ -44,8 +44,8 @@ void GLTFLoader::loadScene(const std::string filename) {
}
} else {
// Re-adjusting camera to fit the new scene
CameraManip.fit(m_gltfScene.m_dimensions.min, m_gltfScene.m_dimensions.max,
true);
//CameraManip.fit(m_gltfScene.m_dimensions.min, m_gltfScene.m_dimensions.max,
// true);
}

m_isSceneLoaded = true;
Expand Down
14 changes: 6 additions & 8 deletions src/config/static_config.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#ifndef __VOLUME_RESTIR_CONFIG_STATIC_CONFIG_HPP__
#define __VOLUME_RESTIR_CONFIG_STATIC_CONFIG_HPP__
#pragma once

#include <string>
#include <vector>

#define VOLUME_RESTIR_USE_VDB
#define USE_GLTF
// #define USE_RT_PIPELINE
#define USE_RESTIR_PIPELINE
//#define USE_VDB
#define USE_GLTF
//#define USE_RT_PIPELINE
#define USE_RESTIR_PIPELINE
// #define USE_ANIMATION

namespace static_config {

Expand All @@ -28,8 +28,6 @@ constexpr size_t kNumGBuffers = 2;

} // namespace static_config

#endif /* __VOLUME_RESTIR_CONFIG_STATIC_CONFIG_HPP__ */

// descSetLayout should also contain uniforms

// std::vector<VkDescriptorSetLayout> rtDescSetLayouts{
Expand Down
58 changes: 45 additions & 13 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace fs = std::filesystem;
// Default search path for shaders
std::vector<std::string> defaultSearchPaths;

const std::string vdb_filename = "cube.vdb";
const std::string vdb_filename = "fire.vdb";
const fs::path asset_dir = fs::path(PROJECT_DIRECTORY) / fs::path("assets");
const std::string file = (asset_dir / vdb_filename).string();

Expand Down Expand Up @@ -86,10 +86,6 @@ static int const SAMPLE_HEIGHT = 720;
int main(int argc, char** argv) {
UNUSED(argc);

#ifdef VOLUME_RESTIR_USE_VDB
SingletonManager::GetVDBLoader().Load(file);
#endif // VOLUME_RESTIR_USE_VDB

// Setup GLFW window
glfwSetErrorCallback(onErrorCallback);
if (!glfwInit()) {
Expand All @@ -101,7 +97,7 @@ int main(int argc, char** argv) {

// Setup camera
CameraManip.setWindowSize(SAMPLE_WIDTH, SAMPLE_HEIGHT);
CameraManip.setLookat(nvmath::vec3f(20, 20, 20), nvmath::vec3f(0, 1, 0),
CameraManip.setLookat(nvmath::vec3f(1, 1, 1), nvmath::vec3f(0, 1, 0),
nvmath::vec3f(0, 1, 0));

// Setup Vulkan
Expand Down Expand Up @@ -189,6 +185,9 @@ int main(int argc, char** argv) {
// Use a compatible device
vkctx.initDevice(compatibleDevices[0], contextInfo);

// clear one line
std::cout << "\n";

// Create example
Renderer renderer;

Expand All @@ -208,7 +207,7 @@ int main(int argc, char** argv) {
renderer.initGUI(0); // Using sub-pass 0

#ifdef USE_GLTF
renderer.loadGLTFModel(nvh::findFile(gltf_cornell, defaultSearchPaths, true));
renderer.loadGLTFModel(nvh::findFile(gltf_sponza, defaultSearchPaths, true));
renderer.createGLTFBuffer();
#else
// Creation of the example
Expand All @@ -219,13 +218,16 @@ int main(int argc, char** argv) {
renderer.createObjDescriptionBuffer();
#endif

#ifdef VOLUME_RESTIR_USE_VDB
#ifdef USE_VDB
SingletonManager::GetVDBLoader().Load(file);
renderer.createVDBBuffer();
#endif // VOLUME_RESTIR_USE_VDB
#endif // USE_VDB

renderer.createOffscreenRender();
renderer.createDescriptorSetLayout();
#ifdef USE_RT_PIPELINE
renderer.createGraphicsPipeline();
#endif
renderer.createUniformBuffer();
renderer.updateDescriptorSet();

Expand All @@ -237,14 +239,17 @@ int main(int argc, char** argv) {
renderer.createBottomLevelAS();
renderer.createTopLevelAS();
renderer.createRtDescriptorSet();
#ifdef USE_RT_PIPELINE
renderer.createRtPipeline(); // Binding m_rtDescSetLayout, m_descSetLayout
renderer.createRtShaderBindingTable();
#endif

renderer.createPostDescriptor();
renderer.createPostPipeline(); // Push Constant float // Binding
// m_postDescSetLayout
renderer.updatePostDescriptorSet();

#ifdef USE_RESTIR_PIPELINE
// # ReSTIR pipeline toggle
// restir lights
renderer.createRestirLights();
Expand Down Expand Up @@ -276,13 +281,22 @@ int main(int argc, char** argv) {
// m_restirDescSetLayout,
// m_restirPostDescSetLayout
renderer.updateRestirDescriptorSet();
#endif

nvmath::vec4f clearColor = nvmath::vec4f(1, 1, 1, 1.00f);
bool useRaytracer = true;

renderer.setupGlfwCallbacks(window);
ImGui_ImplGlfw_InitForVulkan(window, true);

#ifdef USE_ANIMATION
// #VK_compute
renderer.createCompDescriptors();
renderer.createCompPipelines();
#endif

auto start = std::chrono::system_clock::now();

// Main loop
while (!glfwWindowShouldClose(window)) {
glfwPollEvents();
Expand All @@ -307,6 +321,14 @@ int main(int argc, char** argv) {
ImGuiH::Panel::End();
}

std::chrono::duration<float> diff =
std::chrono::system_clock::now() - start;

#ifdef USE_ANIMATION
renderer.animationObject(diff.count());

#endif

// Start rendering the scene
renderer.prepareFrame();

Expand All @@ -329,6 +351,8 @@ int main(int argc, char** argv) {
{clearColor[0], clearColor[1], clearColor[2], clearColor[3]}};
clearValues[1].depthStencil = {1.0f, 0};

renderer.updateFrame();

#ifdef USE_RT_PIPELINE
// Offscreen render pass
{
Expand Down Expand Up @@ -382,10 +406,10 @@ int main(int argc, char** argv) {
cmdBuf, renderer.getRtDescSet(), renderer.getDescSet(),
renderer.getRestirUniformDescSet(), renderer.getLightDescSet(),
renderer.getRestirDescSet(), clearColor);
// renderer.getSpatialReusePass().run(
// cmdBuf, renderer.getRtDescSet(), renderer.getDescSet(),
// renderer.getRestirUniformDescSet(), renderer.getLightDescSet(),
// renderer.getRestirDescSet());
renderer.getSpatialReusePass().run(
cmdBuf, renderer.getRtDescSet(), renderer.getDescSet(),
renderer.getRestirUniformDescSet(), renderer.getLightDescSet(),
renderer.getRestirDescSet());
}

// Restir Pipeline post processing
Expand All @@ -402,6 +426,10 @@ int main(int argc, char** argv) {
// Rendering tonemapper
vkCmdBeginRenderPass(cmdBuf, &restirPostRenderPassBeginInfo,
VK_SUBPASS_CONTENTS_INLINE);
vkCmdPushConstants(cmdBuf, renderer.getRestirPostPipelineLayout(),
VK_SHADER_STAGE_FRAGMENT_BIT, 0,
sizeof(PushConstantRestir),
renderer.getRestirPostPipelinePC());
renderer.restirDrawPost(cmdBuf);
// Rendering UI
ImGui::Render();
Expand All @@ -410,6 +438,10 @@ int main(int argc, char** argv) {
}
#endif

if (renderer.getRestirPostPipelinePC()->frame > 10) {
renderer.getRestirPostPipelinePC()->initialize = 0;
}

// Submit for display
vkEndCommandBuffer(cmdBuf);
renderer.submitFrame();
Expand Down
4 changes: 3 additions & 1 deletion src/passes/restirPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ void RestirPass::run(const VkCommandBuffer& cmdBuf,
0, nullptr);

// Initializing push constant values
m_pcRestir.clearColor = clearColor;
m_pcRestir.clearColorRed = clearColor.x;
m_pcRestir.clearColorGreen = clearColor.y;
m_pcRestir.clearColorBlue = clearColor.z;

vkCmdBindPipeline(cmdBuf, VK_PIPELINE_BIND_POINT_RAY_TRACING_KHR, m_pipeline);

Expand Down
2 changes: 1 addition & 1 deletion src/passes/restirPass.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class RestirPass {
const nvh::GltfScene* m_scene = nullptr;

// push constant for restir
PushConstantRestir m_pcRestir{0, 1};
PushConstantRestir m_pcRestir{0.f, 0.f, 0.f, 0, 1};

nvvk::Buffer m_SBTBuffer;
VkStridedDeviceAddressRegionKHR m_rgenRegion{};
Expand Down
Loading

0 comments on commit 3f90de4

Please sign in to comment.