/* * Copyright (c) 2014-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) 2014-2021 NVIDIA CORPORATION * SPDX-License-Identifier: Apache-2.0 */ #include #include extern std::vector defaultSearchPaths; #define VMA_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION #include "obj_loader.h" #include "stb_image.h" #include "hello_vulkan.h" #include "nvh/cameramanipulator.hpp" #include "nvvk/descriptorsets_vk.hpp" #include "nvvk/images_vk.hpp" #include "nvvk/pipeline_vk.hpp" #include "nvh/fileoperations.hpp" #include "nvvk/commands_vk.hpp" #include "nvvk/renderpasses_vk.hpp" // Holding the camera matrices struct CameraMatrices { nvmath::mat4f view; nvmath::mat4f proj; nvmath::mat4f viewInverse; // #VKRay nvmath::mat4f projInverse; }; //-------------------------------------------------------------------------------------------------- // Keep the handle on the device // Initialize the tool to do all our allocations: buffers, images // void HelloVulkan::setup(const vk::Instance& instance, const vk::Device& device, const vk::PhysicalDevice& physicalDevice, uint32_t queueFamily) { AppBase::setup(instance, device, physicalDevice, queueFamily); m_alloc.init(instance, device, physicalDevice); m_debug.setup(m_device); m_offscreen.setup(device, physicalDevice, &m_alloc, queueFamily); m_raytrace.setup(device, physicalDevice, &m_alloc, queueFamily); } //-------------------------------------------------------------------------------------------------- // Called at each frame to update the camera matrix // void HelloVulkan::updateUniformBuffer(const vk::CommandBuffer& cmdBuf) { // Prepare new UBO contents on host. const float aspectRatio = m_size.width / static_cast(m_size.height); CameraMatrices hostUBO = {}; hostUBO.view = CameraManip.getMatrix(); hostUBO.proj = nvmath::perspectiveVK(CameraManip.getFov(), aspectRatio, 0.1f, 1000.0f); // hostUBO.proj[1][1] *= -1; // Inverting Y for Vulkan (not needed with perspectiveVK). hostUBO.viewInverse = nvmath::invert(hostUBO.view); // #VKRay hostUBO.projInverse = nvmath::invert(hostUBO.proj); // UBO on the device, and what stages access it. vk::Buffer deviceUBO = m_cameraMat.buffer; auto uboUsageStages = vk::PipelineStageFlagBits::eVertexShader | vk::PipelineStageFlagBits::eRayTracingShaderKHR; // Ensure that the modified UBO is not visible to previous frames. vk::BufferMemoryBarrier beforeBarrier; beforeBarrier.setSrcAccessMask(vk::AccessFlagBits::eShaderRead); beforeBarrier.setDstAccessMask(vk::AccessFlagBits::eTransferWrite); beforeBarrier.setBuffer(deviceUBO); beforeBarrier.setOffset(0); beforeBarrier.setSize(sizeof hostUBO); cmdBuf.pipelineBarrier(uboUsageStages, vk::PipelineStageFlagBits::eTransfer, vk::DependencyFlagBits::eDeviceGroup, {}, {beforeBarrier}, {}); // Schedule the host-to-device upload. (hostUBO is copied into the cmd // buffer so it is okay to deallocate when the function returns). cmdBuf.updateBuffer(m_cameraMat.buffer, 0, hostUBO); // Making sure the updated UBO will be visible. vk::BufferMemoryBarrier afterBarrier; afterBarrier.setSrcAccessMask(vk::AccessFlagBits::eTransferWrite); afterBarrier.setDstAccessMask(vk::AccessFlagBits::eShaderRead); afterBarrier.setBuffer(deviceUBO); afterBarrier.setOffset(0); afterBarrier.setSize(sizeof hostUBO); cmdBuf.pipelineBarrier(vk::PipelineStageFlagBits::eTransfer, uboUsageStages, vk::DependencyFlagBits::eDeviceGroup, {}, {afterBarrier}, {}); } //-------------------------------------------------------------------------------------------------- // Describing the layout pushed when rendering // void HelloVulkan::createDescriptorSetLayout() { using vkDS = vk::DescriptorSetLayoutBinding; using vkDT = vk::DescriptorType; using vkSS = vk::ShaderStageFlagBits; uint32_t nbTxt = static_cast(m_textures.size()); uint32_t nbObj = static_cast(m_objModel.size()); // Camera matrices (binding = 0) m_descSetLayoutBind.addBinding( vkDS(0, vkDT::eUniformBuffer, 1, vkSS::eVertex | vkSS::eRaygenKHR)); // Materials (binding = 1) m_descSetLayoutBind.addBinding( vkDS(1, vkDT::eStorageBuffer, nbObj + 1, // Adding Implicit mat too vkSS::eVertex | vkSS::eFragment | vkSS::eClosestHitKHR | vkSS::eAnyHitKHR)); // Scene description (binding = 2) m_descSetLayoutBind.addBinding( // vkDS(2, vkDT::eStorageBuffer, 1, vkSS::eVertex | vkSS::eFragment | vkSS::eClosestHitKHR | vkSS::eAnyHitKHR)); // Textures (binding = 3) m_descSetLayoutBind.addBinding( vkDS(3, vkDT::eCombinedImageSampler, nbTxt, vkSS::eFragment | vkSS::eClosestHitKHR)); // Materials (binding = 4) m_descSetLayoutBind.addBinding(vkDS(4, vkDT::eStorageBuffer, nbObj, vkSS::eFragment | vkSS::eClosestHitKHR | vkSS::eAnyHitKHR)); // Storing vertices (binding = 5) m_descSetLayoutBind.addBinding( // vkDS(5, vkDT::eStorageBuffer, nbObj, vkSS::eClosestHitKHR | vkSS::eAnyHitKHR)); // Storing indices (binding = 6) m_descSetLayoutBind.addBinding( // vkDS(6, vkDT::eStorageBuffer, nbObj, vkSS::eClosestHitKHR | vkSS::eAnyHitKHR)); // Storing implicit obj (binding = 7) m_descSetLayoutBind.addBinding( // vkDS(7, vkDT::eStorageBuffer, 1, vkSS::eClosestHitKHR | vkSS::eIntersectionKHR | vkSS::eAnyHitKHR)); m_descSetLayout = m_descSetLayoutBind.createLayout(m_device); m_descPool = m_descSetLayoutBind.createPool(m_device, 1); m_descSet = nvvk::allocateDescriptorSet(m_device, m_descPool, m_descSetLayout); } //-------------------------------------------------------------------------------------------------- // Setting up the buffers in the descriptor set // void HelloVulkan::updateDescriptorSet() { std::vector writes; // Camera matrices and scene description vk::DescriptorBufferInfo dbiUnif{m_cameraMat.buffer, 0, VK_WHOLE_SIZE}; writes.emplace_back(m_descSetLayoutBind.makeWrite(m_descSet, 0, &dbiUnif)); vk::DescriptorBufferInfo dbiSceneDesc{m_sceneDesc.buffer, 0, VK_WHOLE_SIZE}; writes.emplace_back(m_descSetLayoutBind.makeWrite(m_descSet, 2, &dbiSceneDesc)); // All material buffers, 1 buffer per OBJ std::vector dbiMat; std::vector dbiMatIdx; std::vector dbiVert; std::vector dbiIdx; for(auto& model : m_objModel) { dbiMat.emplace_back(model.matColorBuffer.buffer, 0, VK_WHOLE_SIZE); dbiMatIdx.emplace_back(model.matIndexBuffer.buffer, 0, VK_WHOLE_SIZE); dbiVert.emplace_back(model.vertexBuffer.buffer, 0, VK_WHOLE_SIZE); dbiIdx.emplace_back(model.indexBuffer.buffer, 0, VK_WHOLE_SIZE); } dbiMat.emplace_back(m_implObjects.implMatBuf.buffer, 0, VK_WHOLE_SIZE); // Adding implicit mat writes.emplace_back(m_descSetLayoutBind.makeWriteArray(m_descSet, 1, dbiMat.data())); writes.emplace_back(m_descSetLayoutBind.makeWriteArray(m_descSet, 4, dbiMatIdx.data())); writes.emplace_back(m_descSetLayoutBind.makeWriteArray(m_descSet, 5, dbiVert.data())); writes.emplace_back(m_descSetLayoutBind.makeWriteArray(m_descSet, 6, dbiIdx.data())); // All texture samplers std::vector diit; for(auto& texture : m_textures) { diit.emplace_back(texture.descriptor); } writes.emplace_back(m_descSetLayoutBind.makeWriteArray(m_descSet, 3, diit.data())); vk::DescriptorBufferInfo dbiImplDesc{m_implObjects.implBuf.buffer, 0, VK_WHOLE_SIZE}; writes.emplace_back(m_descSetLayoutBind.makeWrite(m_descSet, 7, &dbiImplDesc)); // Writing the information m_device.updateDescriptorSets(static_cast(writes.size()), writes.data(), 0, nullptr); } //-------------------------------------------------------------------------------------------------- // Creating the pipeline layout // void HelloVulkan::createGraphicsPipeline() { using vkSS = vk::ShaderStageFlagBits; vk::PushConstantRange pushConstantRanges = {vkSS::eVertex | vkSS::eFragment, 0, sizeof(ObjPushConstants)}; // Creating the Pipeline Layout vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo; vk::DescriptorSetLayout descSetLayout(m_descSetLayout); pipelineLayoutCreateInfo.setSetLayoutCount(1); pipelineLayoutCreateInfo.setPSetLayouts(&descSetLayout); pipelineLayoutCreateInfo.setPushConstantRangeCount(1); pipelineLayoutCreateInfo.setPPushConstantRanges(&pushConstantRanges); m_pipelineLayout = m_device.createPipelineLayout(pipelineLayoutCreateInfo); // Creating the Pipeline std::vector paths = defaultSearchPaths; nvvk::GraphicsPipelineGeneratorCombined gpb(m_device, m_pipelineLayout, m_offscreen.renderPass()); gpb.depthStencilState.depthTestEnable = true; gpb.addShader(nvh::loadFile("spv/vert_shader.vert.spv", true, paths, true), vkSS::eVertex); gpb.addShader(nvh::loadFile("spv/frag_shader.frag.spv", true, paths, true), vkSS::eFragment); gpb.addBindingDescription({0, sizeof(VertexObj)}); gpb.addAttributeDescriptions(std::vector{ {0, 0, vk::Format::eR32G32B32Sfloat, offsetof(VertexObj, pos)}, {1, 0, vk::Format::eR32G32B32Sfloat, offsetof(VertexObj, nrm)}, {2, 0, vk::Format::eR32G32B32Sfloat, offsetof(VertexObj, color)}, {3, 0, vk::Format::eR32G32Sfloat, offsetof(VertexObj, texCoord)}}); m_graphicsPipeline = gpb.createPipeline(); m_debug.setObjectName(m_graphicsPipeline, "Graphics"); } //-------------------------------------------------------------------------------------------------- // Loading the OBJ file and setting up all buffers // void HelloVulkan::loadModel(const std::string& filename, nvmath::mat4f transform) { using vkBU = vk::BufferUsageFlagBits; LOGI("Loading File: %s \n", filename.c_str()); ObjLoader loader; loader.loadModel(filename); // Converting from Srgb to linear for(auto& m : loader.m_materials) { m.ambient = nvmath::pow(m.ambient, 2.2f); m.diffuse = nvmath::pow(m.diffuse, 2.2f); m.specular = nvmath::pow(m.specular, 2.2f); } ObjInstance instance; instance.objIndex = static_cast(m_objModel.size()); instance.transform = transform; instance.transformIT = nvmath::transpose(nvmath::invert(transform)); instance.txtOffset = static_cast(m_textures.size()); ObjModel model; model.nbIndices = static_cast(loader.m_indices.size()); model.nbVertices = static_cast(loader.m_vertices.size()); // Create the buffers on Device and copy vertices, indices and materials nvvk::CommandPool cmdBufGet(m_device, m_graphicsQueueIndex); vk::CommandBuffer cmdBuf = cmdBufGet.createCommandBuffer(); model.vertexBuffer = m_alloc.createBuffer(cmdBuf, loader.m_vertices, vkBU::eVertexBuffer | vkBU::eStorageBuffer | vkBU::eShaderDeviceAddress | vkBU::eAccelerationStructureBuildInputReadOnlyKHR); model.indexBuffer = m_alloc.createBuffer(cmdBuf, loader.m_indices, vkBU::eIndexBuffer | vkBU::eStorageBuffer | vkBU::eShaderDeviceAddress | vkBU::eAccelerationStructureBuildInputReadOnlyKHR); model.matColorBuffer = m_alloc.createBuffer(cmdBuf, loader.m_materials, vkBU::eStorageBuffer); model.matIndexBuffer = m_alloc.createBuffer(cmdBuf, loader.m_matIndx, vkBU::eStorageBuffer); // Creates all textures found createTextureImages(cmdBuf, loader.m_textures); cmdBufGet.submitAndWait(cmdBuf); m_alloc.finalizeAndReleaseStaging(); std::string objNb = std::to_string(instance.objIndex); m_debug.setObjectName(model.vertexBuffer.buffer, (std::string("vertex_" + objNb).c_str())); m_debug.setObjectName(model.indexBuffer.buffer, (std::string("index_" + objNb).c_str())); m_debug.setObjectName(model.matColorBuffer.buffer, (std::string("mat_" + objNb).c_str())); m_debug.setObjectName(model.matIndexBuffer.buffer, (std::string("matIdx_" + objNb).c_str())); m_objModel.emplace_back(model); m_objInstance.emplace_back(instance); } //-------------------------------------------------------------------------------------------------- // Creating the uniform buffer holding the camera matrices // - Buffer is host visible // void HelloVulkan::createUniformBuffer() { using vkBU = vk::BufferUsageFlagBits; using vkMP = vk::MemoryPropertyFlagBits; m_cameraMat = m_alloc.createBuffer(sizeof(CameraMatrices), vkBU::eUniformBuffer | vkBU::eTransferDst, vkMP::eDeviceLocal); m_debug.setObjectName(m_cameraMat.buffer, "cameraMat"); } //-------------------------------------------------------------------------------------------------- // Create a storage buffer containing the description of the scene elements // - Which geometry is used by which instance // - Transformation // - Offset for texture // void HelloVulkan::createSceneDescriptionBuffer() { using vkBU = vk::BufferUsageFlagBits; nvvk::CommandPool cmdGen(m_device, m_graphicsQueueIndex); auto cmdBuf = cmdGen.createCommandBuffer(); m_sceneDesc = m_alloc.createBuffer(cmdBuf, m_objInstance, vkBU::eStorageBuffer); cmdGen.submitAndWait(cmdBuf); m_alloc.finalizeAndReleaseStaging(); m_debug.setObjectName(m_sceneDesc.buffer, "sceneDesc"); } //-------------------------------------------------------------------------------------------------- // Creating all textures and samplers // void HelloVulkan::createTextureImages(const vk::CommandBuffer& cmdBuf, const std::vector& textures) { using vkIU = vk::ImageUsageFlagBits; vk::SamplerCreateInfo samplerCreateInfo{ {}, vk::Filter::eLinear, vk::Filter::eLinear, vk::SamplerMipmapMode::eLinear}; samplerCreateInfo.setMaxLod(FLT_MAX); vk::Format format = vk::Format::eR8G8B8A8Srgb; // If no textures are present, create a dummy one to accommodate the pipeline layout if(textures.empty() && m_textures.empty()) { nvvk::Texture texture; std::array color{255u, 255u, 255u, 255u}; vk::DeviceSize bufferSize = sizeof(color); auto imgSize = vk::Extent2D(1, 1); auto imageCreateInfo = nvvk::makeImage2DCreateInfo(imgSize, format); // Creating the VKImage nvvk::Image image = m_alloc.createImage(cmdBuf, bufferSize, color.data(), imageCreateInfo); vk::ImageViewCreateInfo ivInfo = nvvk::makeImageViewCreateInfo(image.image, imageCreateInfo); texture = m_alloc.createTexture(image, ivInfo, samplerCreateInfo); // The image format must be in VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL nvvk::cmdBarrierImageLayout(cmdBuf, texture.image, vk::ImageLayout::eUndefined, vk::ImageLayout::eShaderReadOnlyOptimal); m_textures.push_back(texture); } else { // Uploading all images for(const auto& texture : textures) { std::stringstream o; int texWidth, texHeight, texChannels; o << "media/textures/" << texture; std::string txtFile = nvh::findFile(o.str(), defaultSearchPaths, true); stbi_uc* stbi_pixels = stbi_load(txtFile.c_str(), &texWidth, &texHeight, &texChannels, STBI_rgb_alpha); std::array color{255u, 0u, 255u, 255u}; stbi_uc* pixels = stbi_pixels; // Handle failure if(!stbi_pixels) { texWidth = texHeight = 1; texChannels = 4; pixels = reinterpret_cast(color.data()); } vk::DeviceSize bufferSize = static_cast(texWidth) * texHeight * sizeof(uint8_t) * 4; auto imgSize = vk::Extent2D(texWidth, texHeight); auto imageCreateInfo = nvvk::makeImage2DCreateInfo(imgSize, format, vkIU::eSampled, true); { nvvk::Image image = m_alloc.createImage(cmdBuf, bufferSize, pixels, imageCreateInfo); nvvk::cmdGenerateMipmaps(cmdBuf, image.image, format, imgSize, imageCreateInfo.mipLevels); vk::ImageViewCreateInfo ivInfo = nvvk::makeImageViewCreateInfo(image.image, imageCreateInfo); nvvk::Texture texture = m_alloc.createTexture(image, ivInfo, samplerCreateInfo); m_textures.push_back(texture); } stbi_image_free(stbi_pixels); } } } //-------------------------------------------------------------------------------------------------- // Destroying all allocations // void HelloVulkan::destroyResources() { m_device.destroy(m_graphicsPipeline); m_device.destroy(m_pipelineLayout); m_device.destroy(m_descPool); m_device.destroy(m_descSetLayout); m_alloc.destroy(m_cameraMat); m_alloc.destroy(m_sceneDesc); m_alloc.destroy(m_implObjects.implBuf); m_alloc.destroy(m_implObjects.implMatBuf); for(auto& m : m_objModel) { m_alloc.destroy(m.vertexBuffer); m_alloc.destroy(m.indexBuffer); m_alloc.destroy(m.matColorBuffer); m_alloc.destroy(m.matIndexBuffer); } for(auto& t : m_textures) { m_alloc.destroy(t); } //#Post m_offscreen.destroy(); // #VKRay m_raytrace.destroy(); m_alloc.deinit(); } //-------------------------------------------------------------------------------------------------- // Drawing the scene in raster mode // void HelloVulkan::rasterize(const vk::CommandBuffer& cmdBuf) { using vkPBP = vk::PipelineBindPoint; using vkSS = vk::ShaderStageFlagBits; vk::DeviceSize offset{0}; m_debug.beginLabel(cmdBuf, "Rasterize"); // Dynamic Viewport cmdBuf.setViewport(0, {vk::Viewport(0, 0, (float)m_size.width, (float)m_size.height, 0, 1)}); cmdBuf.setScissor(0, {{{0, 0}, {m_size.width, m_size.height}}}); // Drawing all triangles cmdBuf.bindPipeline(vkPBP::eGraphics, m_graphicsPipeline); cmdBuf.bindDescriptorSets(vkPBP::eGraphics, m_pipelineLayout, 0, {m_descSet}, {}); for(int i = 0; i < m_objInstance.size(); ++i) { auto& inst = m_objInstance[i]; auto& model = m_objModel[inst.objIndex]; m_pushConstants.instanceId = i; // Telling which instance is drawn cmdBuf.pushConstants(m_pipelineLayout, vkSS::eVertex | vkSS::eFragment, 0, m_pushConstants); cmdBuf.bindVertexBuffers(0, {model.vertexBuffer.buffer}, {offset}); cmdBuf.bindIndexBuffer(model.indexBuffer.buffer, 0, vk::IndexType::eUint32); cmdBuf.drawIndexed(model.nbIndices, 1, 0, 0, 0); } m_debug.endLabel(cmdBuf); } //-------------------------------------------------------------------------------------------------- // Handling resize of the window // void HelloVulkan::onResize(int /*w*/, int /*h*/) { m_offscreen.createFramebuffer(m_size); m_offscreen.updateDescriptorSet(); m_raytrace.updateRtDescriptorSet(m_offscreen.colorTexture().descriptor.imageView); resetFrame(); } //-------------------------------------------------------------------------------------------------- // Initialize offscreen rendering // void HelloVulkan::initOffscreen() { m_offscreen.createFramebuffer(m_size); m_offscreen.createDescriptor(); m_offscreen.createPipeline(m_renderPass); m_offscreen.updateDescriptorSet(); } //-------------------------------------------------------------------------------------------------- // Initialize Vulkan ray tracing // void HelloVulkan::initRayTracing() { m_raytrace.createBottomLevelAS(m_objModel, m_implObjects); m_raytrace.createTopLevelAS(m_objInstance, m_implObjects); m_raytrace.createRtDescriptorSet(m_offscreen.colorTexture().descriptor.imageView); m_raytrace.createRtPipeline(m_descSetLayout); } //-------------------------------------------------------------------------------------------------- // Ray trace the scene // void HelloVulkan::raytrace(const vk::CommandBuffer& cmdBuf, const nvmath::vec4f& clearColor) { updateFrame(); if(m_pushConstants.frame >= m_maxFrames) return; m_raytrace.raytrace(cmdBuf, clearColor, m_descSet, m_size, m_pushConstants); } //-------------------------------------------------------------------------------------------------- // If the camera matrix has changed, resets the frame. // otherwise, increments frame. // void HelloVulkan::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_pushConstants.frame++; } void HelloVulkan::resetFrame() { m_pushConstants.frame = -1; } void HelloVulkan::addImplSphere(nvmath::vec3f center, float radius, int matId) { ObjImplicit impl; impl.minimum = center - radius; impl.maximum = center + radius; impl.objType = EObjType::eSphere; impl.matId = matId; m_implObjects.objImpl.push_back(impl); } void HelloVulkan::addImplCube(nvmath::vec3f minumum, nvmath::vec3f maximum, int matId) { ObjImplicit impl; impl.minimum = minumum; impl.maximum = maximum; impl.objType = EObjType::eCube; impl.matId = matId; m_implObjects.objImpl.push_back(impl); } void HelloVulkan::addImplMaterial(const MaterialObj& mat) { m_implObjects.implMat.push_back(mat); } //-------------------------------------------------------------------------------------------------- // Create a storage buffer containing the description of the scene elements // - Which geometry is used by which instance // - Transformation // - Offset for texture // void HelloVulkan::createImplictBuffers() { using vkBU = vk::BufferUsageFlagBits; nvvk::CommandPool cmdGen(m_device, m_graphicsQueueIndex); // Not allowing empty buffers if(m_implObjects.objImpl.empty()) m_implObjects.objImpl.push_back({}); if(m_implObjects.implMat.empty()) m_implObjects.implMat.push_back({}); auto cmdBuf = cmdGen.createCommandBuffer(); m_implObjects.implBuf = m_alloc.createBuffer(cmdBuf, m_implObjects.objImpl, vkBU::eStorageBuffer | vkBU::eShaderDeviceAddress); m_implObjects.implMatBuf = m_alloc.createBuffer(cmdBuf, m_implObjects.implMat, vkBU::eStorageBuffer); cmdGen.submitAndWait(cmdBuf); m_alloc.finalizeAndReleaseStaging(); m_debug.setObjectName(m_implObjects.implBuf.buffer, "implicitObj"); m_debug.setObjectName(m_implObjects.implMatBuf.buffer, "implicitMat"); }