From 3f90de48d8644792912469aee01351d14369ce08 Mon Sep 17 00:00:00 2001 From: Zhihao Ruan Date: Mon, 13 Dec 2021 19:30:44 -0500 Subject: [PATCH] final presentation code release --- CMakeLists.txt | 2 +- src/Renderer.cpp | 419 +++++++++++++++++++++++++++++----- src/Renderer.h | 43 +++- src/Vertex.hpp | 1 + src/common/gltf_loader.cpp | 8 +- src/config/static_config.hpp | 14 +- src/main.cpp | 58 +++-- src/passes/restirPass.cpp | 4 +- src/passes/restirPass.h | 2 +- src/shaders/anim.comp | 81 +++++++ src/shaders/host_device.h | 242 ++++++++++++++++---- src/shaders/raytrace.rint | 14 +- src/shaders/restir.rgen | 6 +- src/shaders/restir.rmiss | 21 +- src/shaders/restir2.rchit | 40 +--- src/shaders/restir_post.frag | 116 ++++------ src/shaders/restircommon.glsl | 6 +- src/shaders/spatialReuse.comp | 46 ---- src/vdb/Types.h | 12 + src/vdb/vdb.cpp | 73 +++++- 20 files changed, 897 insertions(+), 311 deletions(-) create mode 100644 src/shaders/anim.comp diff --git a/CMakeLists.txt b/CMakeLists.txt index e55581b..96100ab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/src/Renderer.cpp b/src/Renderer.cpp index ad33a61..17316f1 100644 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -102,6 +102,7 @@ void Renderer::createGBuffers() { m_gBuffers[i].create(&m_alloc, m_device, m_graphicsQueueIndex, m_size, m_renderPass); } + spdlog::info("Created GBuffers"); } void Renderer::updateGBufferFrameIdx() { @@ -187,6 +188,11 @@ void Renderer::createDescriptorSetLayout() { VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR | VK_SHADER_STAGE_INTERSECTION_BIT_KHR); + m_descSetLayoutBind.addBinding(SceneBindings::eSphereMaterial, + VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, + VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR | + VK_SHADER_STAGE_INTERSECTION_BIT_KHR); + #ifdef USE_GLTF m_descSetLayoutBind.addBinding(SceneBindings::eGLTFVertices, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, @@ -225,6 +231,7 @@ void Renderer::createDescriptorSetLayout() { m_descPool = m_descSetLayoutBind.createPool(m_device, 1); m_descSet = nvvk::allocateDescriptorSet(m_device, m_descPool, m_descSetLayout); + spdlog::info("Created global descriptor set layout"); } //-------------------------------------------------------------------------------------------------- @@ -257,6 +264,12 @@ void Renderer::updateDescriptorSet() { writes.emplace_back( m_descSetLayoutBind.makeWrite(m_descSet, eImplicit, &dbiSpheres)); + VkDescriptorBufferInfo spherematInfo{m_sphereMaterialsBuffer.buffer, 0, + VK_WHOLE_SIZE}; + + writes.emplace_back(m_descSetLayoutBind.makeWrite( + m_descSet, SceneBindings::eSphereMaterial, &spherematInfo)); + #ifdef USE_GLTF // GLTF stuff { @@ -302,6 +315,7 @@ void Renderer::updateDescriptorSet() { // Writing the information vkUpdateDescriptorSets(m_device, static_cast(writes.size()), writes.data(), 0, nullptr); + spdlog::info("Updated global descriptor set"); } //-------------------------------------------------------------------------------------------------- @@ -344,6 +358,7 @@ void Renderer::createGraphicsPipeline() { m_graphicsPipeline = gpb.createPipeline(); m_debug.setObjectName(m_graphicsPipeline, "Graphics"); + spdlog::info("Created Graphics Pipeline"); } void Renderer::loadGLTFModel(const std::string& filename) { @@ -419,12 +434,12 @@ void Renderer::createGLTFBuffer() { // GLTF Scenes m_gltfVertices = m_alloc.createBuffer( cmdBuf, gltfScene.m_positions, - VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | + VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT); m_gltfIndices = m_alloc.createBuffer( cmdBuf, gltfScene.m_indices, - VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | + VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT); m_gltfNormals = m_alloc.createBuffer(cmdBuf, gltfScene.m_normals, @@ -649,6 +664,7 @@ void Renderer::createUniformBuffer() { VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); m_debug.setObjectName(m_bGlobals.buffer, "Globals"); + spdlog::info("Allocated Global Uniform buffer"); } //-------------------------------------------------------------------------------------------------- @@ -845,8 +861,10 @@ void Renderer::createTextureImages(const VkCommandBuffer& cmdBuf, // Destroying all allocations // void Renderer::destroyResources() { +#ifdef USE_RT_PIPELINE vkDestroyPipeline(m_device, m_graphicsPipeline, nullptr); vkDestroyPipelineLayout(m_device, m_pipelineLayout, nullptr); +#endif vkDestroyDescriptorPool(m_device, m_descPool, nullptr); vkDestroyDescriptorSetLayout(m_device, m_descSetLayout, nullptr); @@ -890,10 +908,21 @@ void Renderer::destroyResources() { vkDestroyRenderPass(m_device, m_offscreenRenderPass, nullptr); vkDestroyFramebuffer(m_device, m_offscreenFramebuffer, nullptr); +#ifdef USE_ANIMATION + // Compute + m_alloc.destroy(m_spheresVelocityBuffer); + vkDestroyPipeline(m_device, m_compPipeline, nullptr); + vkDestroyPipelineLayout(m_device, m_compPipelineLayout, nullptr); + vkDestroyDescriptorPool(m_device, m_compDescPool, nullptr); + vkDestroyDescriptorSetLayout(m_device, m_compDescSetLayout, nullptr); +#endif + // #VKRay m_rtBuilder.destroy(); +#ifdef USE_RT_PIPELINE vkDestroyPipeline(m_device, m_rtPipeline, nullptr); vkDestroyPipelineLayout(m_device, m_rtPipelineLayout, nullptr); +#endif vkDestroyDescriptorPool(m_device, m_rtDescPool, nullptr); vkDestroyDescriptorSetLayout(m_device, m_rtDescSetLayout, nullptr); m_alloc.destroy(m_rtSBTBuffer); @@ -904,6 +933,7 @@ void Renderer::destroyResources() { m_alloc.destroy(m_spheresMatColorBuffer); m_alloc.destroy(m_spheresMatIndexBuffer); +#ifdef USE_RESTIR_PIPELINE // restir uniform buffer vkDestroyDescriptorPool(m_device, m_restirUniformDescPool, nullptr); vkDestroyDescriptorSetLayout(m_device, m_restirUniformDescSetLayout, nullptr); @@ -939,6 +969,7 @@ void Renderer::destroyResources() { // ReSTIR passes m_restirPass.destroy(); m_spatialReusePass.destroy(); +#endif // GBuffers for (auto& gBuf : m_gBuffers) { @@ -992,7 +1023,6 @@ void Renderer::onResize(int /*w*/, int /*h*/) { createOffscreenRender(); updatePostDescriptorSet(); updateRtDescriptorSet(); - updateRestirDescriptorSet(); } ////////////////////////////////////////////////////////////////////////// @@ -1136,9 +1166,8 @@ void Renderer::updatePostDescriptorSet() { // the vertex or the fragment program. // void Renderer::createRestirPostDescriptor() { - m_restirPostDescSetLayoutBind.addBinding( - 0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, - VK_SHADER_STAGE_FRAGMENT_BIT); + m_restirPostDescSetLayoutBind.addBinding(0, VK_DESCRIPTOR_TYPE_STORAGE_IMAGE, + 1, VK_SHADER_STAGE_FRAGMENT_BIT); m_restirPostDescSetLayout = m_restirPostDescSetLayoutBind.createLayout(m_device); m_restirPostDescPool = m_restirPostDescSetLayoutBind.createPool(m_device); @@ -1161,13 +1190,19 @@ void Renderer::updateRestirPostDescriptorSet() { // void Renderer::createRestirPostPipeline() { // Creating the pipeline layout + + // pushing time + VkPushConstantRange constants = {VK_SHADER_STAGE_FRAGMENT_BIT, 0, + sizeof(PushConstantRestir)}; std::vector layouts = { m_restirUniformDescSetLayout, m_lightDescSetLayout, m_restirDescSetLayout, m_restirPostDescSetLayout}; VkPipelineLayoutCreateInfo createInfo{ VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO}; - createInfo.setLayoutCount = static_cast(layouts.size()); - createInfo.pSetLayouts = layouts.data(); + createInfo.setLayoutCount = static_cast(layouts.size()); + createInfo.pSetLayouts = layouts.data(); + createInfo.pushConstantRangeCount = 1; + createInfo.pPushConstantRanges = &constants; vkCreatePipelineLayout(m_device, &createInfo, nullptr, &m_restirPostPipelineLayout); @@ -1243,6 +1278,7 @@ void Renderer::initRayTracing() { vkGetPhysicalDeviceProperties2(m_physicalDevice, &prop2); m_rtBuilder.setup(m_device, &m_alloc, m_graphicsQueueIndex); + spdlog::info("Set up m_rtBuilder"); } //-------------------------------------------------------------------------------------------------- @@ -1381,30 +1417,63 @@ void Renderer::createVDBBuffer() { return; } - float scaleFactor = 1; + float scaleFactor = 0.05; nvmath::mat3f scaleMatrix = nvmath::mat3f(1.0); scaleMatrix *= scaleFactor; - nvmath::vec3f translation{0, 5.0f, 0}; + nvmath::vec3f translation{-2.5, 0.5f, 0}; + nvmath::vec3f translation2{1.0f, 0.0f, 0}; uint32_t nbSpheres = static_cast(vdb->AllPoints.size()); + // nbSpheres = 1000; m_spheres.resize(nbSpheres); - for (uint32_t i = 0; i < nbSpheres; i++) { + for (size_t i = 0; i < nbSpheres; i++) { Sphere s; s.center = scaleMatrix * nvmath::vec3f(vdb->AllPoints[i].x, vdb->AllPoints[i].y, vdb->AllPoints[i].z); s.center += translation; - s.radius = 0.1; + s.radius = 0.005; + nvmath::vec3f vel = nvmath::vec3f( + vdb->AllPoints[i].vx, vdb->AllPoints[i].vy, vdb->AllPoints[i].vz); m_spheres[i] = std::move(s); } +#ifdef USE_ANIMATION + int sphereAnimate = nbSpheres / 10; + m_spheresVelocity.resize(nbSpheres); + for (size_t i = 0; i < sphereAnimate; i++) { + Velocity v; + v.velocity = nvmath::vec3f(vdb->AllPoints[i].vx, vdb->AllPoints[i].vy, + vdb->AllPoints[i].vz); + + // s.acceleration = nvmath::vec3f(0.f, -9.8f, 0.f); // gravity + m_spheresVelocity[i] = std::move(v); + } + + for (int i = sphereAnimate; i < nbSpheres; i++) { + Velocity v; + v.velocity = nvmath::vec3f(0.0f, 0.0f, 0.0f); + + // s.acceleration = nvmath::vec3f(0.f, -9.8f, 0.f); // gravity + m_spheresVelocity[i] = std::move(v); + } + +#endif // Axis aligned bounding box of each sphere std::vector aabbs; aabbs.reserve(nbSpheres); for (const auto& s : m_spheres) { Aabb aabb; - aabb.minimum = s.center - nvmath::vec3f(s.radius); - aabb.maximum = s.center + nvmath::vec3f(s.radius); + nvmath::vec3f minimum = s.center - nvmath::vec3f(s.radius); + nvmath::vec3f maximum = s.center + nvmath::vec3f(s.radius); + aabb.minimum_x = minimum.x; + aabb.minimum_y = minimum.y; + aabb.minimum_z = minimum.z; + + aabb.maximum_x = maximum.x; + aabb.maximum_y = maximum.y; + aabb.maximum_z = maximum.z; + aabbs.emplace_back(aabb); } @@ -1413,11 +1482,40 @@ void Renderer::createVDBBuffer() { materials.reserve(nbSpheres); for (size_t i = 0; i < m_spheres.size(); ++i) { MaterialObj mat; - mat.diffuse = nvmath::vec3f(vdb->AllPoints[i].nx, vdb->AllPoints[i].ny, - vdb->AllPoints[i].nz); + mat.diffuse = nvmath::vec3f(vdb->AllPoints[i].cx, vdb->AllPoints[i].cy, + vdb->AllPoints[i].cz); materials.emplace_back(mat); } + // create Sphere GLTF MAetrial + for (size_t i = 0; i < m_spheres.size(); ++i) { + // Create Material for Sphere + GltfMaterials spheremat; + spheremat.pbrBaseColorFactor = nvmath::normalize( + nvmath::vec4(vdb->AllPoints[i].cx, vdb->AllPoints[i].cy, + vdb->AllPoints[i].cz, 1)); // Main Color + spheremat.pbrBaseColorTexture = 0.0001f; // For mettalic Color + spheremat.pbrMetallicFactor = 0.0001f; // For mettalic factor + spheremat.pbrRoughnessFactor = 0.9; + spheremat.pbrMetallicRoughnessTexture = -1; + spheremat.khrDiffuseFactor = + nvmath::vec4(0.5, 0.1, 0.2, 1); //// Main Color + spheremat.khrSpecularFactor = nvmath::vec3(0.5, 0.5, 0.5); // Specular + spheremat.khrDiffuseTexture = -1; // emissiveTexture make it -2 + spheremat.shadingModel = SHADING_MODEL_SPECULAR_GLOSSINESS; // + spheremat.khrGlossinessFactor = 0.3; + spheremat.khrSpecularGlossinessTexture = -1; + spheremat.emissiveTexture = -1; // emissiveTexture make it -2 + spheremat.emissiveFactor = nvmath::vec3(0.3, 0.3, 0.3); // emmisiveFactor + // spheremat.alphaMode = m.alphaMode; + // spheremat.alphaCutoff = m.alphaCutoff; + // spheremat.doubleSided = m.doubleSided; + // spheremat.normalTexture = m.normalTexture; + // spheremat.normalTextureScale = m.normalTextureScale; + // spheremat.uvTransform = m.uvTransform; + m_sphereMaterials.emplace_back(spheremat); + } + // ORIGINAL -- Creating two materials // // MaterialObj mat; @@ -1435,31 +1533,37 @@ void Renderer::createVDBBuffer() { } // Creating all buffers + VkBufferUsageFlags flag = VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT; + VkBufferUsageFlags + rayTracingFlags = // used also for building acceleration structures + flag | + VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR | + VK_BUFFER_USAGE_STORAGE_BUFFER_BIT; using vkBU = VkBufferUsageFlagBits; nvvk::CommandPool genCmdBuf(m_device, m_graphicsQueueIndex); auto cmdBuf = genCmdBuf.createCommandBuffer(); m_spheresBuffer = m_alloc.createBuffer(cmdBuf, m_spheres, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT); - m_spheresAabbBuffer = m_alloc.createBuffer( - cmdBuf, aabbs, - VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT | - VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR); - m_spheresMatIndexBuffer = - m_alloc.createBuffer(cmdBuf, matIdx, - VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | - VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT); - m_spheresMatColorBuffer = - m_alloc.createBuffer(cmdBuf, materials, - VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | - VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT); + m_spheresAabbBuffer = m_alloc.createBuffer(cmdBuf, aabbs, rayTracingFlags); + m_spheresMatIndexBuffer = m_alloc.createBuffer( + cmdBuf, matIdx, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | flag); + m_spheresMatColorBuffer = m_alloc.createBuffer( + cmdBuf, materials, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | flag); + m_sphereMaterialsBuffer = m_alloc.createBuffer( + cmdBuf, m_sphereMaterials, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | flag); + +#ifdef USE_ANIMATION + m_spheresVelocityBuffer = m_alloc.createBuffer( + cmdBuf, m_spheresVelocity, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | flag); +#endif genCmdBuf.submitAndWait(cmdBuf); - m_alloc.finalizeAndReleaseStaging(); // Debug information m_debug.setObjectName(m_spheresBuffer.buffer, "spheres"); m_debug.setObjectName(m_spheresAabbBuffer.buffer, "spheresAabb"); m_debug.setObjectName(m_spheresMatColorBuffer.buffer, "spheresMat"); m_debug.setObjectName(m_spheresMatIndexBuffer.buffer, "spheresMatIdx"); + m_debug.setObjectName(m_sphereMaterialsBuffer.buffer, "spheresMatIdx"); // Adding an extra instance to get access to the material buffers ObjDesc objDesc{}; @@ -1470,8 +1574,11 @@ void Renderer::createVDBBuffer() { m_objDesc.emplace_back(objDesc); ObjInstance instance{}; - instance.objIndex = static_cast(m_objModel.size()); + instance.transform = nvmath::mat4f(1.f); + instance.objIndex = static_cast(m_objModel.size()); m_instances.emplace_back(instance); + + spdlog::info("VDB Buffer created"); } //-------------------------------------------------------------------------------------------------- @@ -1485,11 +1592,53 @@ void Renderer::createRestirLights() { if (m_pointLights.empty()) { // generate random point lights - m_pointLights = generatePointLights(nvmath::vec3(-10, -10, -10), - nvmath::vec3(10, 10, 10)); - spdlog::info("Generated point lights of size {}", m_pointLights.size()); + nvmath::vec3f min_range, max_range; + if (SingletonManager::GetGLTFLoader().isSceneLoaded()) { + auto gltfScene = SingletonManager::GetGLTFLoader().getGLTFScene(); + m_pointLights = generatePointLights(gltfScene.m_dimensions.min, + gltfScene.m_dimensions.max); + min_range = gltfScene.m_dimensions.min; + max_range = gltfScene.m_dimensions.max; + } else { + m_pointLights = generatePointLights(nvmath::vec3f(-10, -10, -10), + nvmath::vec3f(10, 10, 10)); + min_range = nvmath::vec3f(-10, -10, -10); + max_range = nvmath::vec3f(10, 10, 10); + } + spdlog::info("Generated point lights of size {} in range {}, {} :", + m_pointLights.size(), min_range, max_range); } + m_pointLights = generatePointLights(nvmath::vec3f(-10, -10, -10), + nvmath::vec3f(10, 10, 10), false, 1000); + + VDB* vdb = nullptr; + int lightCounter = 0; + if (SingletonManager::GetVDBLoader().IsVDBLoaded()) { + vdb = SingletonManager::GetVDBLoader().GetPtr(); + + uint32_t nbSpheres = static_cast(vdb->AllPoints.size()); + for (int i = 0; i < nbSpheres; i++) { + if (lightCounter > 1000) { + break; + } + if (vdb->AllPoints[i].temp > 275) { + lightCounter++; + PointLight currLight; + currLight.pos = m_spheres[i].center; + currLight.emission_luminance = nvmath::vec4f(0.6, 0.2, 0.1, 1.0); + + currLight.emission_luminance.w = shader::luminance( + currLight.emission_luminance.x, currLight.emission_luminance.y, + currLight.emission_luminance.z); + m_pointLights.push_back(currLight); + } + } + } + + // min_range = nvmath::vec3f(-10, -10, -10); + // max_range = nvmath::vec3f(10, 10, 10); + if (m_triangleLights.empty()) { // triangle lights are only created with a GLTF scene // m_triangleLights = collectTriangleLights(gltfScene); @@ -1546,11 +1695,10 @@ void Renderer::createRestirLights() { // void Renderer::createBottomLevelAS() { // BLAS - Storing each primitive in a geometry - std::vector allBlas; #ifdef USE_GLTF const auto gltfScene = SingletonManager::GetGLTFLoader().getGLTFScene(); - allBlas.resize(gltfScene.m_primMeshes.size() + 1); + allBlas.reserve(gltfScene.m_primMeshes.size() + 1); for (auto& primMesh : gltfScene.m_primMeshes) { auto geo = gltfToGeometryKHR(m_device, primMesh); allBlas.push_back({geo}); @@ -1566,32 +1714,30 @@ void Renderer::createBottomLevelAS() { #endif // Spheres -#ifdef VOLUME_RESTIR_USE_VDB +#ifdef USE_VDB { auto blas = sphereToVkGeometryKHR(); allBlas.emplace_back(blas); } -#endif // VOLUME_RESTIR_USE_VDB +#endif // USE_VDB m_rtBuilder.buildBlas( - allBlas, VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR); + allBlas, VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_UPDATE_BIT_KHR | + VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_BUILD_BIT_KHR); + spdlog::info("Created BLAS for ray tracing"); } //-------------------------------------------------------------------------------------------------- // // void Renderer::createTopLevelAS() { - std::vector tlas; -#ifdef VOLUME_RESTIR_USE_VDB - auto nbObj = static_cast(m_instances.size()) - 1; -#else - auto nbObj = static_cast(m_instances.size()); -#endif // VOLUME_RESTIR_USE_VDB - #ifdef USE_GLTF const auto gltfScene = SingletonManager::GetGLTFLoader().getGLTFScene(); +#ifdef USE_VDB tlas.reserve(gltfScene.m_nodes.size() + 1); - uint32_t instID = 0; +#else + tlas.reserve(gltfScene.m_nodes.size()); +#endif // USE_VDB for (auto& node : gltfScene.m_nodes) { VkAccelerationStructureInstanceKHR rayInst; rayInst.transform = nvvk::toTransformMatrixKHR(node.worldMatrix); @@ -1606,8 +1752,12 @@ void Renderer::createTopLevelAS() { tlas.emplace_back(rayInst); } #else - tlas.reserve(nbObj + 1); - for (uint32_t i = 0; i < nbObj; i++) { +#ifdef USE_VDB + tlas.reserve(m_objModel.size() + 1); +#else + tlas.reserve(m_objModel.size()); +#endif + for (size_t i = 0; i < m_objModel.size(); i++) { const auto& inst = m_instances[i]; VkAccelerationStructureInstanceKHR rayInst{}; @@ -1624,30 +1774,35 @@ void Renderer::createTopLevelAS() { } #endif // Add the blas containing all implicit objects -#ifdef VOLUME_RESTIR_USE_VDB +#ifdef USE_VDB { + SphereBlasID = gltfScene.m_primMeshes.size(); VkAccelerationStructureInstanceKHR rayInst{}; rayInst.transform = nvvk::toTransformMatrixKHR(nvmath::mat4f(1)); // (identity) #ifdef USE_GLTF rayInst.instanceCustomIndex = static_cast(gltfScene.m_primMeshes.size()); + rayInst.accelerationStructureReference = m_rtBuilder.getBlasDeviceAddress( + static_cast(gltfScene.m_primMeshes.size())); #else rayInst.instanceCustomIndex = static_cast( - m_instances.size()); // nbObj == last object == implicit -#endif + m_objModel.size()); // nbObj == last object == implicit rayInst.accelerationStructureReference = m_rtBuilder.getBlasDeviceAddress( static_cast(m_objModel.size())); +#endif rayInst.flags = VK_GEOMETRY_INSTANCE_TRIANGLE_FACING_CULL_DISABLE_BIT_KHR; rayInst.mask = 0xFF; // Only be hit if rayMask & instance.mask != 0 rayInst.instanceShaderBindingTableRecordOffset = 1; // We will use the same hit group for all objects tlas.emplace_back(rayInst); } -#endif // VOLUME_RESTIR_USE_VDB +#endif // USE_VDB m_rtBuilder.buildTlas( - tlas, VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_TRACE_BIT_KHR); + tlas, VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_UPDATE_BIT_KHR | + VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_BUILD_BIT_KHR); + spdlog::info("Created TLAS for ray tracing"); } //-------------------------------------------------------------------------------------------------- @@ -1689,6 +1844,7 @@ void Renderer::createRtDescriptorSet() { m_rtDescSet, RtxBindings::eOutImage, &imageInfo)); vkUpdateDescriptorSets(m_device, static_cast(writes.size()), writes.data(), 0, nullptr); + spdlog::info("Created Ray Tracing AccelerationStructure descriptor set"); } //-------------------------------------------------------------------------------------------------- @@ -1733,6 +1889,7 @@ void Renderer::createLightDescriptorSet() { vkUpdateDescriptorSets(m_device, static_cast(writes.size()), writes.data(), 0, nullptr); + spdlog::info("Created ReSTIR Light descriptor set"); } void Renderer::createRestirDescriptorSet() { @@ -1814,6 +1971,7 @@ void Renderer::createRestirDescriptorSet() { nvvk::allocateDescriptorSets(m_device, m_restirDescPool, m_restirDescSetLayout, static_config::kNumGBuffers, m_restirDescSets); + spdlog::info("Created ReSTIR (reservoir) descriptor set"); } void Renderer::updateRestirDescriptorSet() { @@ -1848,10 +2006,10 @@ void Renderer::updateRestirDescriptorSet() { set, RestirBindings::ePrevFrameMaterialProps, &bufprev.getMaterialPropertiesTexture().descriptor)); - VkDescriptorImageInfo imgInfo{ - {}, m_storageImage.descriptor.imageView, VK_IMAGE_LAYOUT_GENERAL}; - writes.emplace_back(m_restirDescSetLayoutBind.makeWrite( - set, RestirBindings::eStorageImage, &imgInfo)); + // VkDescriptorImageInfo imgInfo{ + // {}, m_storageImage.descriptor.imageView, VK_IMAGE_LAYOUT_GENERAL}; + // writes.emplace_back(m_restirDescSetLayoutBind.makeWrite( + // set, RestirBindings::eStorageImage, &imgInfo)); writes.emplace_back(m_restirDescSetLayoutBind.makeWrite( set, RestirBindings::eReservoirsInfo, @@ -1878,6 +2036,7 @@ void Renderer::updateRestirDescriptorSet() { } vkUpdateDescriptorSets(m_device, static_cast(writes.size()), writes.data(), 0, nullptr); + spdlog::info("Updated ReSTIR (reservoir) descriptor set"); } //-------------------------------------------------------------------------------------------------- @@ -2032,6 +2191,7 @@ void Renderer::createRtPipeline() { &m_rtPipeline); for (auto& s : stages) vkDestroyShaderModule(m_device, s.module, nullptr); + spdlog::info("Created ray tracing pipeline"); } //-------------------------------------------------------------------------------------------------- @@ -2117,6 +2277,7 @@ void Renderer::createRtShaderBindingTable() { m_alloc.unmap(m_rtSBTBuffer); m_alloc.finalizeAndReleaseStaging(); + spdlog::info("Created ray tracing SBT"); } //-------------------------------------------------------------------------------------------------- @@ -2159,6 +2320,7 @@ void Renderer::createRestirPipeline() { m_restirPass.createPipeline(m_rtDescSetLayout, m_descSetLayout, m_restirUniformDescSetLayout, m_lightDescSetLayout, m_restirDescSetLayout); + spdlog::info("Created ReSTIR pass pipeline"); } //-------------------------------------------------------------------------------------------------- @@ -2171,19 +2333,20 @@ void Renderer::createSpatialReusePipeline() { m_spatialReusePass.createPipeline( m_rtDescSetLayout, m_descSetLayout, m_restirUniformDescSetLayout, m_lightDescSetLayout, m_restirDescSetLayout); + spdlog::info("Created Spatial Reuse pass pipeline"); } void Renderer::createRestirUniformBuffer() { const float aspectRatio = m_size.width / static_cast(m_size.height); m_restirUniforms.debugMode = 0; - m_restirUniforms.gamma = 2.2; // const but i think can be changed by UI + m_restirUniforms.gamma = 4.0; // const but i think can be changed by UI m_restirUniforms.screenSize = nvmath::vec2ui(m_size.width, m_size.height); m_restirUniforms.flags = RESTIR_VISIBILITY_REUSE_FLAG | RESTIR_TEMPORAL_REUSE_FLAG | RESTIR_SPATIAL_REUSE_FLAG; // const m_restirUniforms.spatialNeighbors = 4; // const m_restirUniforms.spatialRadius = 30.0f; // const - m_restirUniforms.initialLightSampleCount = (1 << 5); // const + m_restirUniforms.initialLightSampleCount = (1 << 6); // const m_restirUniforms.pointLightCount = static_cast(m_pointLights.size()); // const m_restirUniforms.triangleLightCount = @@ -2207,6 +2370,7 @@ void Renderer::createRestirUniformBuffer() { VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); m_debug.setObjectName(m_restirUniformBuffer.buffer, "restirUniformBuffer"); + spdlog::info("Created ReSTIR uniform buffer"); } void Renderer::updateRestirUniformBuffer(const VkCommandBuffer& cmdBuf) { @@ -2286,4 +2450,141 @@ void Renderer::updateRestirUniformDescriptorSet() { // Writing the information vkUpdateDescriptorSets(m_device, static_cast(writes.size()), writes.data(), 0, nullptr); + spdlog::info("Created ReSTIR uniform descriptor set"); +} + +void Renderer::updateFrame() { + static nvmath::mat4f refCamMatrix; + static float refFov{CameraManip.getFov()}; + + const auto& m = CameraManip.getMatrix(); + const auto fov = CameraManip.getFov(); + + if (memcmp(&refCamMatrix.a00, &m.a00, sizeof(nvmath::mat4f)) != 0 || + refFov != fov) { + resetFrame(); + refCamMatrix = m; + refFov = fov; + } + m_pcRestirPost.frame++; +} + +void Renderer::resetFrame() { m_pcRestirPost.frame = -1; } +////////////////////////////////////////////////////////////////////////// +// #VK_compute +void Renderer::createCompDescriptors() { + m_compDescSetLayoutBind.addBinding(0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, + VK_SHADER_STAGE_COMPUTE_BIT); + m_compDescSetLayoutBind.addBinding(1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, + VK_SHADER_STAGE_COMPUTE_BIT); + m_compDescSetLayoutBind.addBinding(2, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1, + VK_SHADER_STAGE_COMPUTE_BIT); + + m_compDescSetLayout = m_compDescSetLayoutBind.createLayout(m_device); + m_compDescPool = m_compDescSetLayoutBind.createPool(m_device, 1); + m_compDescSet = nvvk::allocateDescriptorSet(m_device, m_compDescPool, + m_compDescSetLayout); +} + +void Renderer::updateCompDescriptors(nvvk::Buffer& a_spheresAabbBuffer, + nvvk::Buffer& a_spheres) { + std::vector writes; + + VkDescriptorBufferInfo dbiUnifAabb{a_spheresAabbBuffer.buffer, 0, + VK_WHOLE_SIZE}; + writes.emplace_back( + m_compDescSetLayoutBind.makeWrite(m_compDescSet, 0, &dbiUnifAabb)); + + VkDescriptorBufferInfo dbiUnifSphere{a_spheres.buffer, 0, VK_WHOLE_SIZE}; + writes.emplace_back( + m_compDescSetLayoutBind.makeWrite(m_compDescSet, 1, &dbiUnifSphere)); + + VkDescriptorBufferInfo dbiUnifVelocity{m_spheresVelocityBuffer.buffer, 0, + VK_WHOLE_SIZE}; + writes.emplace_back( + m_compDescSetLayoutBind.makeWrite(m_compDescSet, 2, &dbiUnifVelocity)); + + vkUpdateDescriptorSets(m_device, static_cast(writes.size()), + writes.data(), 0, nullptr); +} + +void Renderer::createCompPipelines() { + // pushing time + VkPushConstantRange push_constants = {VK_SHADER_STAGE_COMPUTE_BIT, 0, + sizeof(CompPushConstant)}; + + VkPipelineLayoutCreateInfo createInfo{ + VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO}; + createInfo.setLayoutCount = 1; + createInfo.pSetLayouts = &m_compDescSetLayout; + createInfo.pushConstantRangeCount = 1; + createInfo.pPushConstantRanges = &push_constants; + vkCreatePipelineLayout(m_device, &createInfo, nullptr, &m_compPipelineLayout); + + VkComputePipelineCreateInfo computePipelineCreateInfo{ + VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO}; + computePipelineCreateInfo.layout = m_compPipelineLayout; + + computePipelineCreateInfo.stage = nvvk::createShaderStageInfo( + m_device, + nvh::loadFile("spv/anim.comp.spv", true, defaultSearchPaths, true), + VK_SHADER_STAGE_COMPUTE_BIT); + + vkCreateComputePipelines(m_device, {}, 1, &computePipelineCreateInfo, nullptr, + &m_compPipeline); + + updateCompDescriptors(m_spheresAabbBuffer, m_spheresBuffer); + + vkDestroyShaderModule(m_device, computePipelineCreateInfo.stage.module, + nullptr); +} + +//-------------------------------------------------------------------------------------------------- +// Animating the sphere vertices using a compute shader +// +void Renderer::animationObject(float t1) { + // const uint32_t sphereId = + // 1; // Get from where we are loading spheres, HArd code for now + + nvvk::CommandPool genCmdBuf(m_device, m_graphicsQueueIndex); + VkCommandBuffer cmdBuf = genCmdBuf.createCommandBuffer(); + + vkCmdBindPipeline(cmdBuf, VK_PIPELINE_BIND_POINT_COMPUTE, m_compPipeline); + vkCmdBindDescriptorSets(cmdBuf, VK_PIPELINE_BIND_POINT_COMPUTE, + m_compPipelineLayout, 0, 1, &m_compDescSet, 0, + nullptr); + float deltaT; + if (t1 < t0) { + deltaT = t1; + } else { + deltaT = t1 - t0; + } + t0 = t1; + CompPushConstant abc{deltaT, m_spheres.size()}; + // CompPushConstant abc{t1, 9920}; + vkCmdPushConstants(cmdBuf, m_compPipelineLayout, VK_SHADER_STAGE_COMPUTE_BIT, + 0, sizeof(CompPushConstant), &abc); + + // int height = WORKGROUP_SIZE; + // int width = m_spheres.size()/ (1024 * 512) + 1; + + int total = m_spheres.size(); + int width = std::ceil(total / 1024); + + vkCmdDispatch(cmdBuf, width, 1, + 1); // size of sphere is the size we need + + genCmdBuf.submitAndWait(cmdBuf); + m_rtBuilder.updateBlas( + SphereBlasID, allBlas[SphereBlasID], + VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_UPDATE_BIT_KHR | + VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_BUILD_BIT_KHR); + + VkAccelerationStructureInstanceKHR& tinst = tlas[SphereBlasID]; + tinst.transform = nvvk::toTransformMatrixKHR(nvmath::mat4f(1)); + m_rtBuilder.buildTlas( + tlas, + VK_BUILD_ACCELERATION_STRUCTURE_ALLOW_UPDATE_BIT_KHR | + VK_BUILD_ACCELERATION_STRUCTURE_PREFER_FAST_BUILD_BIT_KHR, + true); } diff --git a/src/Renderer.h b/src/Renderer.h index 0e61787..b274fac 100644 --- a/src/Renderer.h +++ b/src/Renderer.h @@ -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& getSpheres() { return m_spheres; } PushConstantRaster& getPushConstant() { return m_pcRaster; } @@ -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{ @@ -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 m_objModel; // Model on host @@ -166,6 +183,12 @@ class Renderer : public nvvk::AppBaseVk { VkPipelineLayout m_pipelineLayout; VkPipeline m_graphicsPipeline; + // Acceleration structures + std::vector tlas; + std::vector allBlas; + + int SphereBlasID; + nvvk::Buffer m_bGlobals; // Device-Host of the camera matrices nvvk::Buffer m_bObjDesc; // Device buffer of the OBJ descriptions @@ -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}; @@ -229,7 +252,7 @@ class Renderer : public nvvk::AppBaseVk { std::vector 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; @@ -282,6 +305,9 @@ 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 m_spheres; // All spheres nvvk::Buffer m_spheresBuffer; // Buffer holding the spheres nvvk::Buffer m_spheresAabbBuffer; // Buffer of all Aabb @@ -289,6 +315,19 @@ class Renderer : public nvvk::AppBaseVk { nvvk::Buffer m_spheresMatIndexBuffer; // Define which sphere uses which material + //#VKCompute + std::vector 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 m_sphereMaterials; + nvvk::Buffer m_sphereMaterialsBuffer; }; diff --git a/src/Vertex.hpp b/src/Vertex.hpp index b767bf1..8b7f2ad 100644 --- a/src/Vertex.hpp +++ b/src/Vertex.hpp @@ -13,6 +13,7 @@ #include #include + #include namespace volume_restir { diff --git a/src/common/gltf_loader.cpp b/src/common/gltf_loader.cpp index 2c7fd04..e44cab1 100644 --- a/src/common/gltf_loader.cpp +++ b/src/common/gltf_loader.cpp @@ -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)}); @@ -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; diff --git a/src/config/static_config.hpp b/src/config/static_config.hpp index 9560d45..852d78d 100644 --- a/src/config/static_config.hpp +++ b/src/config/static_config.hpp @@ -1,13 +1,13 @@ -#ifndef __VOLUME_RESTIR_CONFIG_STATIC_CONFIG_HPP__ -#define __VOLUME_RESTIR_CONFIG_STATIC_CONFIG_HPP__ +#pragma once #include #include -#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 { @@ -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 rtDescSetLayouts{ diff --git a/src/main.cpp b/src/main.cpp index 0b43919..6875acc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -46,7 +46,7 @@ namespace fs = std::filesystem; // Default search path for shaders std::vector 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(); @@ -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()) { @@ -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 @@ -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; @@ -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 @@ -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(); @@ -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(); @@ -276,6 +281,7 @@ 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; @@ -283,6 +289,14 @@ int main(int argc, char** argv) { 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(); @@ -307,6 +321,14 @@ int main(int argc, char** argv) { ImGuiH::Panel::End(); } + std::chrono::duration diff = + std::chrono::system_clock::now() - start; + +#ifdef USE_ANIMATION + renderer.animationObject(diff.count()); + +#endif + // Start rendering the scene renderer.prepareFrame(); @@ -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 { @@ -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 @@ -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(); @@ -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(); diff --git a/src/passes/restirPass.cpp b/src/passes/restirPass.cpp index 158c41e..634978d 100644 --- a/src/passes/restirPass.cpp +++ b/src/passes/restirPass.cpp @@ -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); diff --git a/src/passes/restirPass.h b/src/passes/restirPass.h index 8eb9a16..96c2721 100644 --- a/src/passes/restirPass.h +++ b/src/passes/restirPass.h @@ -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{}; diff --git a/src/shaders/anim.comp b/src/shaders/anim.comp new file mode 100644 index 0000000..c9ab50a --- /dev/null +++ b/src/shaders/anim.comp @@ -0,0 +1,81 @@ +#version 460 +#extension GL_ARB_separate_shader_objects : enable +#extension GL_GOOGLE_include_directive : enable +#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require +#extension GL_EXT_debug_printf : enable +#include "wavefront.glsl" + +layout(local_size_x = 1024, local_size_y = 1, local_size_z = 1) in; + +layout(set = 0, binding = 0) buffer SphereAabb_ { Aabb aabb[]; } +Aabbs; + +layout(set = 0, binding = 1) buffer Sphere_ { Sphere s[]; } +Spheres; + +layout(set = 0, binding = 2) buffer Velocity_ { Velocity v[]; } +velocities; + +// vec3 center; +// float radius; +// float density; +// vec3 origin; +// vec3 velocity; +// vec3 acceleration; + +layout(push_constant) uniform shaderInformation { + float deltaTime; + int size; +} +pushc; +float scalor = 0.05f; +void main() { + // debugPrintfEXT("My float is %f", velocity); + + uint currIdx = gl_GlobalInvocationID.x; + if (currIdx > pushc.size) return; + + // ----------------- Testing Comp ----------------- // + + // uint i = currIdx; + // for (uint i = currIdx; i <= pushc.Size; i += 1024) { + + // Sphere currsphere = Spheres.s[i]; + // Aabb old_aabb = Aabbs.aabb[i]; + + // vec3 moveSpeed = vec3( float(sin(pushc.time/ 1000.0)) * 0.3f, 0.0f, 0.0f); + // Spheres.s[i].center += moveSpeed; + // Aabbs.aabb[i].minimum_x += moveSpeed.x; + // Aabbs.aabb[i].minimum_y += moveSpeed.y; + // Aabbs.aabb[i].minimum_z += moveSpeed.z; + // Aabbs.aabb[i].maximum_x += moveSpeed.x; + // Aabbs.aabb[i].maximum_y += moveSpeed.y; + // Aabbs.aabb[i].maximum_z += moveSpeed.z; + + //} + + // ------------------- Bunny ------------------- // + + // ----------------- Explosion ----------------- // + + uint i = currIdx; + // Sphere currSphere = Spheres.s[i]; + // vec3 deltaPos = vec3(0.1f, 0.1f, 0.1f) * scalor * pushc.deltaTime; + + // vec3 velocity = vec3( Spheres.s[i].velocity_x, + // Spheres.s[i].velocity_y, Spheres.s[i].velocity_z); + vec3 deltaPos = + normalize(velocities.v[i].velocity) * scalor * pushc.deltaTime; + + vec3 finalPos = Spheres.s[i].center + deltaPos; + + Spheres.s[i].center = finalPos; + Aabbs.aabb[i].minimum_x += deltaPos.x; + Aabbs.aabb[i].minimum_y += deltaPos.y; + Aabbs.aabb[i].minimum_z += deltaPos.z; + Aabbs.aabb[i].maximum_x += deltaPos.x; + Aabbs.aabb[i].maximum_y += deltaPos.y; + Aabbs.aabb[i].maximum_z += deltaPos.z; + + barrier(); +} \ No newline at end of file diff --git a/src/shaders/host_device.h b/src/shaders/host_device.h index c423503..c983618 100644 --- a/src/shaders/host_device.h +++ b/src/shaders/host_device.h @@ -54,7 +54,8 @@ START_BINDING(SceneBindings) eGLTFMaterials = 10, eGLTFTextures = 11, eGLTFMatrices = 12, - eGLTFPrimLookup = 13 + eGLTFPrimLookup = 13, + eSphereMaterial = 14 END_BINDING(); START_BINDING(RtxBindings) @@ -100,7 +101,132 @@ END_BINDING(); #define ALPHA_MODE_MASK 1 #define ALPHA_MODE_BLEND 2 +#ifdef __cplusplus // Information of a obj model when referenced in a shader +struct ObjDesc { + alignas(4) int txtOffset; // Texture index offset in the array of textures + alignas(8) uint64_t vertexAddress; // Address of the Vertex buffer + alignas(8) uint64_t indexAddress; // Address of the index buffer + alignas(8) uint64_t materialAddress; // Address of the material buffer + alignas(8) uint64_t + materialIndexAddress; // Address of the triangle material index buffer +}; + +// Uniform buffer set at each frame +struct GlobalUniforms { + alignas(64) mat4 viewProj; // Camera view * projection + alignas(64) mat4 viewInverse; // Camera inverse view matrix + alignas(64) mat4 projInverse; // Camera inverse projection matrix +}; + +// Push constant structure for the raster +struct PushConstantRaster { + alignas(64) mat4 modelMatrix; // matrix of the instance + alignas(16) vec3 lightPosition; + alignas(4) uint objIndex; + alignas(4) float lightIntensity; + alignas(4) int lightType; +}; + +// Push constant structure for the ray tracer +struct PushConstantRay { + alignas(16) vec4 clearColor; + alignas(16) vec3 lightPosition; + alignas(4) float lightIntensity; + alignas(4) int lightType; +}; + +struct PushConstantRestir { + alignas(4) float clearColorRed; + alignas(4) float clearColorGreen; + alignas(4) float clearColorBlue; + alignas(4) int frame; + alignas(4) int initialize; +}; + +struct Vertex // See ObjLoader, copy of VertexObj, could be compressed for + // device +{ + alignas(16) vec3 pos; + alignas(16) vec3 nrm; + alignas(16) vec3 color; + alignas(8) vec2 texCoord; +}; + +struct WaveFrontMaterial // See ObjLoader, copy of MaterialObj, could be + // compressed for device +{ + alignas(16) vec3 ambient; + alignas(16) vec3 diffuse; + alignas(16) vec3 specular; + alignas(16) vec3 transmittance; + alignas(16) vec3 emission; + alignas(4) float shininess; + alignas(4) float ior; // index of refraction + alignas(4) float dissolve; // 1 == opaque; 0 == fully transparent + alignas(4) int illum; // illumination model (see + // http://www.fileformat.info/format/material/) + alignas(4) int textureId; +}; + +struct GLTFModelMatrices { + alignas(64) mat4 transform; + alignas(64) mat4 transformInverseTransposed; +}; + +struct RestirPrimitiveLookup { + alignas(4) uint indexOffset; + alignas(4) uint vertexOffset; + alignas(4) int materialIndex; +}; + +// ReSTIR params +struct PointLight { // m_lightDescSetLayoutBind + alignas(16) vec4 pos; + alignas(16) vec4 emission_luminance; // w is luminance +}; + +struct TriangleLight { // m_lightDescSetLayoutBind + alignas(16) vec4 p1; + alignas(16) vec4 p2; + alignas(16) vec4 p3; + alignas(16) vec4 emission_luminance; // w is luminance + alignas(16) vec4 normalArea; +}; + +struct AliasTableCell { // m_lightDescSetLayoutBind + alignas(4) int alias; + alignas(4) float prob; + alignas(4) float pdf; + alignas(4) float aliasPdf; +}; + +struct RestirUniforms { // m_restirUniformDescSetLayoutBind + alignas(4) int pointLightCount; + alignas(4) int triangleLightCount; + alignas(4) int aliasTableCount; + + alignas(4) float environmentalPower; + alignas(4) float fireflyClampThreshold; + + alignas(4) uint spatialNeighbors; + alignas(4) float spatialRadius; + + alignas(4) uint initialLightSampleCount; + alignas(4) int temporalSampleCountMultiplier; + + alignas(8) uvec2 screenSize; + alignas(16) vec4 currCamPos; + alignas(64) mat4 currFrameProjectionViewMatrix; + alignas(16) vec4 prevCamPos; + alignas(64) mat4 prevFrameProjectionViewMatrix; + + alignas(4) int flags; + alignas(4) int debugMode; + alignas(4) float gamma; +}; + +#else struct ObjDesc { int txtOffset; // Texture index offset in the array of textures uint64_t vertexAddress; // Address of the Vertex buffer @@ -135,9 +261,11 @@ struct PushConstantRay { }; struct PushConstantRestir { + float clearColorRed; + float clearColorGreen; + float clearColorBlue; int frame; int initialize; - vec4 clearColor; }; struct Vertex // See ObjLoader, copy of VertexObj, could be compressed for @@ -165,33 +293,6 @@ struct WaveFrontMaterial // See ObjLoader, copy of MaterialObj, could be int textureId; }; -struct GltfMaterials { - int shadingModel; - vec4 pbrBaseColorFactor; - - int pbrBaseColorTexture; - float pbrMetallicFactor; - float pbrRoughnessFactor; - int pbrMetallicRoughnessTexture; - - // KHR_materials_pbrSpecularGlossiness - vec4 khrDiffuseFactor; - vec3 khrSpecularFactor; - int khrDiffuseTexture; - float khrGlossinessFactor; - int khrSpecularGlossinessTexture; - - int emissiveTexture; - vec3 emissiveFactor; - int alphaMode; - - float alphaCutoff; - int doubleSided; - int normalTexture; - float normalTextureScale; - mat4 uvTransform; -}; - struct GLTFModelMatrices { mat4 transform; mat4 transformInverseTransposed; @@ -203,16 +304,6 @@ struct RestirPrimitiveLookup { int materialIndex; }; -struct Sphere { - vec3 center; - float radius; -}; - -struct Aabb { - vec3 minimum; - vec3 maximum; -}; - // ReSTIR params struct PointLight { // m_lightDescSetLayoutBind vec4 pos; @@ -259,6 +350,77 @@ struct RestirUniforms { // m_restirUniformDescSetLayoutBind float gamma; }; +#endif + +struct Sphere { + vec3 center; + float radius; +}; + +struct Velocity { + vec3 velocity; +}; + +struct Aabb { + float minimum_x; + float minimum_y; + float minimum_z; + float maximum_x; + float maximum_y; + float maximum_z; +}; + +struct CompPushConstant { + float time; + int size; +}; + +#ifdef __cplusplus +struct GltfMaterials { + alignas(4) int shadingModel; + alignas(4) int pbrBaseColorTexture; + alignas(4) float pbrMetallicFactor; + alignas(4) float pbrRoughnessFactor; + alignas(4) int pbrMetallicRoughnessTexture; + alignas(4) int khrDiffuseTexture; + alignas(4) float khrGlossinessFactor; + alignas(4) int khrSpecularGlossinessTexture; + alignas(4) int emissiveTexture; + alignas(4) int alphaMode; + alignas(4) float alphaCutoff; + alignas(4) int doubleSided; + alignas(4) int normalTexture; + alignas(4) float normalTextureScale; + alignas(16) vec4 pbrBaseColorFactor; + alignas(16) vec4 khrDiffuseFactor; + alignas(16) vec3 khrSpecularFactor; + alignas(16) vec3 emissiveFactor; + alignas(64) mat4 uvTransform; +}; +#else +struct GltfMaterials { + int shadingModel; + int pbrBaseColorTexture; + float pbrMetallicFactor; + float pbrRoughnessFactor; + int pbrMetallicRoughnessTexture; + int khrDiffuseTexture; + float khrGlossinessFactor; + int khrSpecularGlossinessTexture; + int emissiveTexture; + int alphaMode; + float alphaCutoff; + int doubleSided; + int normalTexture; + float normalTextureScale; + vec4 pbrBaseColorFactor; + vec4 khrDiffuseFactor; + vec3 khrSpecularFactor; + vec3 emissiveFactor; + mat4 uvTransform; +}; +#endif + #define KIND_SPHERE 0 #define KIND_CUBE 1 @@ -267,4 +429,4 @@ struct RestirUniforms { // m_restirUniformDescSetLayoutBind #define RESTIR_SPATIAL_REUSE_FLAG (1 << 2) #define USE_ENVIRONMENT_FLAG (1 << 3) -#endif +#endif \ No newline at end of file diff --git a/src/shaders/raytrace.rint b/src/shaders/raytrace.rint index 6ffbc52..e354e32 100644 --- a/src/shaders/raytrace.rint +++ b/src/shaders/raytrace.rint @@ -54,12 +54,14 @@ float hitSphere(const Sphere s, const Ray r) { // Ray-AABB intersection float hitAabb(const Aabb aabb, const Ray r) { vec3 invDir = 1.0 / r.direction; - vec3 tbot = invDir * (aabb.minimum - r.origin); - vec3 ttop = invDir * (aabb.maximum - r.origin); - vec3 tmin = min(ttop, tbot); - vec3 tmax = max(ttop, tbot); - float t0 = max(tmin.x, max(tmin.y, tmin.z)); - float t1 = min(tmax.x, min(tmax.y, tmax.z)); + vec3 tbot = invDir * + (vec3(aabb.minimum_x, aabb.minimum_y, aabb.minimum_z) - r.origin); + vec3 ttop = invDir * + (vec3(aabb.maximum_x, aabb.maximum_y, aabb.maximum_z) - r.origin); + vec3 tmin = min(ttop, tbot); + vec3 tmax = max(ttop, tbot); + float t0 = max(tmin.x, max(tmin.y, tmin.z)); + float t1 = min(tmax.x, min(tmax.y, tmax.z)); return t1 > max(t0, 0.0) ? t0 : -1.0; } diff --git a/src/shaders/restir.rgen b/src/shaders/restir.rgen index 6dbae5b..73b9ce2 100644 --- a/src/shaders/restir.rgen +++ b/src/shaders/restir.rgen @@ -123,7 +123,7 @@ void SceneSample(inout uint seed, vec3 worldPos, out vec3 lightSamplePos, } else { TriangleLight light = triangleLights.lights[selected_idx]; lightSamplePos = getTrianglePoint(rnd(seed), rnd(seed), light.p1.xyz, - light.p2.xyz, light.p3.xyz); + light.p2.xyz, light.p3.xyz); lightSampleLum = light.emission_luminance.w; lightKind = LIGHT_KIND_TRIANGLE; vec3 wi = normalize(worldPos - lightSamplePos); @@ -234,7 +234,7 @@ void main() { } } - if ((restirUniform.flags & RESTIR_TEMPORAL_REUSE_FLAG) != 0) { + /*if ((restirUniform.flags & RESTIR_TEMPORAL_REUSE_FLAG) != 0) { vec4 prevFramePos = restirUniform.prevFrameProjectionViewMatrix * vec4(gInfo.worldPos, 1.0f); prevFramePos.xyz /= prevFramePos.w; @@ -281,7 +281,7 @@ void main() { } } } - } + }*/ vec4 reservoirInfo, reservoirWeight; packReservoirStruct(res, reservoirInfo, reservoirWeight); diff --git a/src/shaders/restir.rmiss b/src/shaders/restir.rmiss index 9321891..b5d17d0 100644 --- a/src/shaders/restir.rmiss +++ b/src/shaders/restir.rmiss @@ -18,11 +18,8 @@ layout(set = 2, binding = eUniform) uniform _RestirUniforms { // environmentalTexture; layout(push_constant) uniform _PushConstantRestir { - int frame; - int initialize; - vec4 clearColor; -} -pushC; + PushConstantRestir pcRestir; +}; vec2 GetSphericalUv(vec3 v) { float gamma = asin(-v.y); @@ -35,10 +32,12 @@ vec2 GetSphericalUv(vec3 v) { void main() { prd.worldPos.w = 0.0; prd.exist = false; - prd.albedo.xyz = pushC.clearColor.xyz * 0.8; - // if ((uniforms.flags & USE_ENVIRONMENT_FLAG) != 0) { - // vec2 uv = GetSphericalUv(gl_WorldRayDirectionEXT.xyz); - // prd.emissive = texture(environmentalTexture, uv).rgb; - // prd.albedo = vec4(1.0); - // } + // prd.albedo.xyz = vec3(pcRestir.clearColorRed, pcRestir.clearColorGreen, + // pcRestir.clearColorBlue) * + // 0.8; + // if ((uniforms.flags & USE_ENVIRONMENT_FLAG) != 0) { + // vec2 uv = GetSphericalUv(gl_WorldRayDirectionEXT.xyz); + // prd.emissive = texture(environmentalTexture, uv).rgb; + // prd.albedo = vec4(1.0); + // } } diff --git a/src/shaders/restir2.rchit b/src/shaders/restir2.rchit index 3e3944f..3e6e5ad 100644 --- a/src/shaders/restir2.rchit +++ b/src/shaders/restir2.rchit @@ -48,6 +48,11 @@ layout(set = 1, binding = eGLTFMatrices) buffer _Matrices { GLTFModelMatrices matrices[]; }; +layout(set = 1, + binding = eSphereMaterial) readonly buffer _SphereMaterialBuffer { + GltfMaterials sphereMaterials[]; +}; + layout(set = 1, binding = eGLTFPrimLookup) readonly buffer _InstanceInfo { RestirPrimitiveLookup primInfo[]; }; @@ -98,40 +103,15 @@ void main() { material.normalTextureScale = 1.0; material.uvTransform = mat4(1.0); - sstate.text_coords = - (vec4(sstate.text_coords.xy, 1, 1) * material.uvTransform).xy; - if (material.normalTexture > -1) { - mat3 TBN = mat3(sstate.tangent_u, sstate.tangent_v, sstate.normal); - vec3 normalVector = - texture(texturesMap[nonuniformEXT(material.normalTexture)], - sstate.text_coords) - .xyz; - normalVector = normalize(normalVector * 2.0 - 1.0); - normalVector *= - vec3(material.normalTextureScale, material.normalTextureScale, 1.0); - sstate.normal = normalize(TBN * normalVector); - } - vec3 emissive = material.emissiveFactor; - if (material.emissiveTexture > -1) - emissive *= - SRGBtoLINEAR( - texture(texturesMap[nonuniformEXT(material.emissiveTexture)], - sstate.text_coords)) - .rgb; - - Material bsdfMat; - if (material.shadingModel == SHADING_MODEL_METALLIC_ROUGHNESS) - bsdfMat = GetMetallicRoughness(material, sstate); - else - bsdfMat = GetSpecularGlossiness(material, sstate); + GltfMaterials currSphereMaterials = sphereMaterials[gl_PrimitiveID]; prd.worldPos.xyz = worldPos; prd.worldNormal = worldNrm; - prd.albedo.xyz = vec3(instance.center); + prd.albedo = currSphereMaterials.pbrBaseColorFactor; prd.worldPos.w = 1.0; - prd.roughness = 0.8; - prd.metallic = 0.8; - prd.emissive = vec3(0.5, 0, 0); + prd.roughness = currSphereMaterials.pbrRoughnessFactor; + prd.metallic = currSphereMaterials.pbrMetallicFactor; + prd.emissive = currSphereMaterials.emissiveFactor; prd.exist = true; // prd.worldPos.xyz = sstate.position; diff --git a/src/shaders/restir_post.frag b/src/shaders/restir_post.frag index 9ce5b39..2a57161 100644 --- a/src/shaders/restir_post.frag +++ b/src/shaders/restir_post.frag @@ -34,7 +34,7 @@ layout(set = 2, binding = eReservoirsInfo, rgba32f) uniform image2D reservoirInfoBuf; layout(set = 2, binding = eReservoirWeights, rgba32f) uniform image2D reservoirWeightBuf; -layout(set = 3, binding = 0) uniform sampler2D resultImage; +layout(set = 3, binding = 0, rgba32f) uniform image2D resultImage; layout(location = 0) in vec2 inUv; @@ -45,11 +45,14 @@ layout(location = 0) out vec3 outColor; #define PI 3.1415926 -// layout(push_constant) uniform Constants { -// int frame; -// int initialize; -// } -// pushC; +layout(push_constant) uniform Constants { + float clearColorRed; + float clearColorGreen; + float clearColorBlue; + int frame; + int initialize; +} +pushC; void main() { ivec2 coordImage = ivec2(gl_FragCoord.xy); @@ -63,71 +66,40 @@ void main() { gInfo.metallic = roughnessMetallic.y; gInfo.camPos = uniforms.currCamPos.xyz; - outColor = normalize(gInfo.albedo.xyz); - - /* if (uniforms.debugMode == DEBUG_NONE) { - uvec2 pixelCoord = uvec2(gl_FragCoord.xy); - - vec4 reservoirInfo = imageLoad(reservoirInfoBuf, coordImage); - vec4 reservoirWeight = imageLoad(reservoirWeightBuf, coordImage); - Reservoir res = unpackReservoirStruct(reservoirInfo, reservoirWeight); - gInfo.sampleSeed = res.sampleSeed; - - uint lightIndex = res.lightIndex; - int lightKind = res.lightKind; - vec3 pHat = evaluatePHatFull(lightIndex, lightKind, gInfo); - outColor += pHat * res.w; - if (gInfo.albedo.w > 0.5f) { - outColor = gInfo.albedo.xyz; - } - } else if (uniforms.debugMode == DEBUG_ALBEDO) { - if (gInfo.albedo.a < 0.5f) { - outColor = gInfo.albedo.rgb; - } else { - outColor = vec3(0.0f); - } - } else if (uniforms.debugMode == DEBUG_EMISSION) { - if (gInfo.albedo.a > 0.5f) { - outColor = gInfo.albedo.rgb; - } else { - outColor = vec3(0.0f); - } - } *//* else if (uniforms.debugMode == DEBUG_NORMAL) { - outColor = (vec3(gInfo.normal) + 1.0f) * 0.5f; - } else if (uniforms.debugMode == DEBUG_ROUGHNESS) { - outColor = vec3(roughnessMetallic.r); - } else if (uniforms.debugMode == DEBUG_METALLIC) { - outColor = vec3(roughnessMetallic.g); - } else if (uniforms.debugMode == DEBUG_WORLD_POSITION) { - outColor = gInfo.worldPos / 10.0f + 0.5f; - } else if (uniforms.debugMode == DEBUG_NAIVE_POINT_LIGHT_NO_SHADOW) { - float roughness = roughnessMetallic.r; - float metallic = roughnessMetallic.g; - - outColor = vec3(0.0f); - for (int i = 0; i < uniforms.pointLightCount; ++i) { - outColor += evaluatePHatFull(uint(i), LIGHT_KIND_POINT, gInfo); - } - } */ - - // { - // float lum = luminance(outColor); - // if (lum > uniforms.fireflyClampThreshold) - // outColor *= uniforms.fireflyClampThreshold / lum; - // } - - // outColor = max(vec3(0.0), outColor); - - // TODO: Uncomment this! - // - // if (pushC.frame < 1 || pushC.initialize == 1) { - // imageStore(resultImage, ivec2(gl_FragCoord.xy), vec4(outColor, 1.f)); - // } else { - // float a = 1.0f / float(pushC.frame); - // vec3 old_color = imageLoad(resultImage, ivec2(gl_FragCoord.xy)).xyz; - // imageStore(resultImage, ivec2(gl_FragCoord.xy), - // vec4(mix(old_color, outColor, a), 1.f)); - // outColor = mix(old_color, outColor, a); - // } + outColor = vec3(0.0f); + + uvec2 pixelCoord = uvec2(gl_FragCoord.xy); + + vec4 reservoirInfo = imageLoad(reservoirInfoBuf, coordImage); + vec4 reservoirWeight = imageLoad(reservoirWeightBuf, coordImage); + Reservoir res = unpackReservoirStruct(reservoirInfo, reservoirWeight); + gInfo.sampleSeed = res.sampleSeed; + + uint lightIndex = res.lightIndex; + int lightKind = res.lightKind; + vec3 pHat = evaluatePHatFull(lightIndex, lightKind, gInfo); + outColor += pHat * res.w; + if (gInfo.albedo.w > 0.5f) { + outColor = gInfo.albedo.xyz; + } + + { + float lum = luminance(outColor); + if (lum > uniforms.fireflyClampThreshold) + outColor *= uniforms.fireflyClampThreshold / lum; + } + + outColor = max(vec3(0.0), outColor); + + if (pushC.frame < 1 || pushC.initialize == 1) { + imageStore(resultImage, ivec2(gl_FragCoord.xy), vec4(outColor, 1.f)); + } else { + float a = 1.0f / float(pushC.frame); + vec3 old_color = imageLoad(resultImage, ivec2(gl_FragCoord.xy)).xyz; + imageStore(resultImage, ivec2(gl_FragCoord.xy), + vec4(mix(old_color, outColor, a), 1.f)); + outColor = mix(old_color, outColor, a); + } // outColor = pow(outColor, vec3(1.0f / uniforms.gamma)); + outColor = pow(outColor, vec3(1.0f / 0.8f)); } diff --git a/src/shaders/restircommon.glsl b/src/shaders/restircommon.glsl index 80d9730..83c97fb 100644 --- a/src/shaders/restircommon.glsl +++ b/src/shaders/restircommon.glsl @@ -127,7 +127,7 @@ ShadeState GetShadeState(in HitState hstate) { vec3 world_tangent = normalize(vec3(mat4(hstate.ObjectToWorld) * vec4(tangent.xyz, 0))); world_tangent = normalize(world_tangent - - dot(world_tangent, world_normal) * world_normal); + dot(world_tangent, world_normal) * world_normal); vec3 world_binormal = cross(world_normal, world_tangent) * tng0.w; // TexCoord @@ -254,8 +254,8 @@ Material GetSpecularGlossiness(GltfMaterials material, ShadeState sstate) { if (material.khrSpecularGlossinessTexture > -1) { vec4 sgSample = SRGBtoLINEAR(texture( - texturesMap[nonuniformEXT(material.khrSpecularGlossinessTexture)], - sstate.text_coords)); + texturesMap[nonuniformEXT(material.khrSpecularGlossinessTexture)], + sstate.text_coords)); perceptualRoughness = 1 - material.khrGlossinessFactor * sgSample.a; // glossiness to roughness f0 *= sgSample.rgb; // specular diff --git a/src/shaders/spatialReuse.comp b/src/shaders/spatialReuse.comp index 64b66e7..1d60f4f 100644 --- a/src/shaders/spatialReuse.comp +++ b/src/shaders/spatialReuse.comp @@ -83,52 +83,6 @@ void main() { vec4 reservoirWeight = imageLoad(reservoirWeightBuf, coordImage); Reservoir res = unpackReservoirStruct(reservoirInfo, reservoirWeight); - if ((uniforms.flags & RESTIR_SPATIAL_REUSE_FLAG) != 0) { - packReservoirStruct(res, reservoirInfo, reservoirWeight); - imageStore(resultReservoirInfoBuf, coordImage, reservoirInfo); - imageStore(resultReservoirWeightBuf, coordImage, reservoirWeight); - return; - } - - for (int i = 0; i < NUM_NEIGHBORS; ++i) { - float angle = rnd(seed) * 2.0 * M_PI; - float radius = sqrt(rnd(seed)) * uniforms.spatialRadius; - ivec2 randNeighbor = ivec2(round(vec2(cos(angle), sin(angle)) * radius)); - randNeighbor = clamp(ivec2(pixelCoord) + randNeighbor, ivec2(0), - ivec2(uniforms.screenSize - 1)); - - GeometryInfo n_gInfo; - - n_gInfo.worldPos = imageLoad(frameWorldPosition, ivec2(randNeighbor)).xyz; - n_gInfo.normal = imageLoad(frameNormal, ivec2(randNeighbor)).xyz; - n_gInfo.albedo = imageLoad(frameAlbedo, ivec2(randNeighbor)); - vec3 neighborRoughnessMetallic = - imageLoad(frameRoughnessMetallic, ivec2(randNeighbor)).xyz; - n_gInfo.roughness = neighborRoughnessMetallic.x; - n_gInfo.metallic = neighborRoughnessMetallic.y; - n_gInfo.albedoLum = - luminance(n_gInfo.albedo.r, n_gInfo.albedo.g, n_gInfo.albedo.b); - n_gInfo.camPos = gInfo.camPos; - - vec3 positionDiff = gInfo.worldPos - n_gInfo.worldPos; - if (dot(positionDiff, positionDiff) < 0.01f) { - vec3 albedoDiff = gInfo.albedo.xyz - n_gInfo.albedo.xyz; - if (dot(albedoDiff, albedoDiff) < 0.01f) { - float normalDot = dot(gInfo.normal, n_gInfo.normal); - if (normalDot > 0.5f) { - uint neighborIndex = - randNeighbor.y * uniforms.screenSize.x + randNeighbor.x; - vec4 reservoirInfo = imageLoad(reservoirInfoBuf, ivec2(randNeighbor)); - vec4 reservoirWeight = - imageLoad(reservoirWeightBuf, ivec2(randNeighbor)); - Reservoir randRes = - unpackReservoirStruct(reservoirInfo, reservoirWeight); - - combineReservoirs(res, randRes, gInfo, n_gInfo, seed); - } - } - } - } packReservoirStruct(res, reservoirInfo, reservoirWeight); imageStore(resultReservoirInfoBuf, coordImage, reservoirInfo); imageStore(resultReservoirWeightBuf, coordImage, reservoirWeight); diff --git a/src/vdb/Types.h b/src/vdb/Types.h index 933b0c5..b524478 100644 --- a/src/vdb/Types.h +++ b/src/vdb/Types.h @@ -61,6 +61,18 @@ struct vDat { GLfloat x; GLfloat y; GLfloat z; + + GLfloat vx; + GLfloat vy; + GLfloat vz; + + GLfloat cx; + GLfloat cy; + GLfloat cz; + + GLfloat d; + + GLfloat temp; }; /// @struct BBoxBare diff --git a/src/vdb/vdb.cpp b/src/vdb/vdb.cpp index 501e75c..8b5d962 100644 --- a/src/vdb/vdb.cpp +++ b/src/vdb/vdb.cpp @@ -760,6 +760,10 @@ void VDB::getMeshValuesScalar(typename GridType::ConstPtr _grid) { /*if (pointChannel() >0) { pointStore = AllPoints; }*/ + bool Creategrid = false; + if (AllPoints.size() == 0) { + Creategrid = true; + } vDat point; @@ -792,20 +796,28 @@ void VDB::getMeshValuesScalar(typename GridType::ConstPtr _grid) { point.u = j; j++; // incremenet point count + if (channelName(pointChannel()) == "density") { + point.d = (float)vec; + } + if (channelName(pointChannel()) == "temperature") { + float tempVal = log((float)vec) + 273.15; + point.temp = tempVal; nvmath::vec3f flameColor = - nvmath::normalize(nvmath::vec3f(300, 88, 34)) * (float)vec * 1000.0f; - point.nx = + nvmath::normalize(nvmath::vec3f(200, 88, 34)) * (float)vec * 1000.f; + point.cx = flameColor[0]; // set colour to normal for rendering on the shader - point.ny = flameColor[1]; - point.nz = flameColor[2]; - } else { + point.cy = flameColor[1]; + point.cz = flameColor[2]; + } + // Give Every Particle color of Smoke + else { nvmath::vec3f smokeColor = - nvmath::normalize(nvmath::vec3f(50, 54, 50)) * (float)vec; - point.nx = + nvmath::normalize(nvmath::vec3f(100, 100, 100)) * (float)vec * 1000.f; + point.cx = smokeColor[0]; // set colour to normal for rendering on the shader - point.ny = smokeColor[1]; - point.nz = smokeColor[2]; + point.cy = smokeColor[1]; + point.cz = smokeColor[2]; } channelTemp[0] = vec; // store value for texture buffer @@ -857,7 +869,20 @@ void VDB::getMeshValuesScalar(typename GridType::ConstPtr _grid) { // else { // AllPoints = pointStore; //} - AllPoints.insert(AllPoints.end(), pointStore.begin(), pointStore.end()); + if (Creategrid) { + AllPoints.insert(AllPoints.end(), pointStore.begin(), pointStore.end()); + } else { + int size = AllPoints.size() > pointStore.size() ? pointStore.size() + : AllPoints.size(); + for (int i = 0; i < size; i++) { + if (channelName(pointChannel()) == "temperature") { + AllPoints[i].cx = pointStore[i].cx; + AllPoints[i].cy = pointStore[i].cy; + AllPoints[i].cz = pointStore[i].cz; + AllPoints[i].temp = pointStore[i].temp; + } + } + } //// create VAO for this grid // temp.bind(); // temp.setIndicesCount(j); @@ -903,11 +928,16 @@ void VDB::getMeshValuesVector(typename GridType::ConstPtr _grid) { vDat point; openvdb::Coord coord; - + bool Creategrid = false; + if (AllPoints.size() == 0) { + Creategrid = true; + } BBoxBare channelExtremes; channelExtremes.minx = channelExtremes.miny = channelExtremes.minz = 0.0f; channelExtremes.maxx = channelExtremes.maxy = channelExtremes.maxz = 0.0f; + std::string channelname = channelName(pointChannel()); + for (typename GridType::ValueOnCIter it = _grid->cbeginValueOn(); it; ++it) { // will always be a rounding issue here as must be an integer step // however this could then cause it to miss out a few hundred thousand @@ -927,6 +957,12 @@ void VDB::getMeshValuesVector(typename GridType::ConstPtr _grid) { vec.normalize(); // normalize vector before setting + if (channelName(pointChannel()) == "v") { + point.vx = vec[0]; + point.vy = vec[1]; + point.vz = vec[2]; + } + point.nx = vec[0]; point.ny = vec[1]; point.nz = vec[2]; @@ -970,6 +1006,21 @@ void VDB::getMeshValuesVector(typename GridType::ConstPtr _grid) { } } } + if (Creategrid) { + AllPoints.insert(AllPoints.end(), pointStore.begin(), pointStore.end()); + } else { + // Since Grid has already been created for density channelwe will just fetch + // all points + int size = AllPoints.size() > pointStore.size() ? pointStore.size() + : AllPoints.size(); + for (int i = 0; i < size; i++) { + if (channelName(pointChannel()) == "v") { + AllPoints[i].vx = pointStore[i].vx; + AllPoints[i].vy = pointStore[i].vy; + AllPoints[i].vz = pointStore[i].vz; + } + } + } // TODO : Very Imp!!! pointStore are directly Being Pushed in VAO so make sure // we do it in vulkan