118 lines
4.6 KiB
C++
118 lines
4.6 KiB
C++
/* Copyright (c) 2014-2018, NVIDIA CORPORATION. All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
* * Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
* * Neither the name of NVIDIA CORPORATION nor the names of its
|
|
* contributors may be used to endorse or promote products derived
|
|
* from this software without specific prior written permission.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
|
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
|
|
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
#pragma once
|
|
|
|
#include "vkalloc.hpp"
|
|
|
|
#include "nvvkpp/allocator_dedicated_vkpp.hpp"
|
|
#include "nvvkpp/appbase_vkpp.hpp"
|
|
#include "nvvkpp/debug_util_vkpp.hpp"
|
|
|
|
// #VKRay
|
|
#include "nvvkpp/raytraceKHR_vkpp.hpp"
|
|
#include "offscreen.hpp"
|
|
|
|
#include "obj.hpp"
|
|
#include "raytrace.hpp"
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
// Simple rasterizer of OBJ objects
|
|
// - Each OBJ loaded are stored in an `ObjModel` and referenced by a `ObjInstance`
|
|
// - It is possible to have many `ObjInstance` referencing the same `ObjModel`
|
|
// - Rendering is done in an offscreen framebuffer
|
|
// - The image of the framebuffer is displayed in post-process in a full-screen quad
|
|
//
|
|
class HelloVulkan : public nvvkpp::AppBase
|
|
{
|
|
public:
|
|
void setup(const vk::Device& device,
|
|
const vk::PhysicalDevice& physicalDevice,
|
|
uint32_t queueFamily) override;
|
|
void createDescriptorSetLayout();
|
|
void createGraphicsPipeline();
|
|
void loadModel(const std::string& filename, nvmath::mat4f transform = nvmath::mat4f(1));
|
|
void updateDescriptorSet();
|
|
void createUniformBuffer();
|
|
void createSceneDescriptionBuffer();
|
|
void createTextureImages(const vk::CommandBuffer& cmdBuf,
|
|
const std::vector<std::string>& textures);
|
|
void updateUniformBuffer();
|
|
void onResize(int /*w*/, int /*h*/) override;
|
|
void destroyResources();
|
|
void rasterize(const vk::CommandBuffer& cmdBuff);
|
|
|
|
Offscreen& offscreen() { return m_offscreen; }
|
|
Raytracer& raytracer() { return m_raytrace; }
|
|
|
|
ObjPushConstants m_pushConstants;
|
|
|
|
// Array of objects and instances in the scene
|
|
std::vector<ObjModel> m_objModel;
|
|
std::vector<ObjInstance> m_objInstance;
|
|
|
|
|
|
// Graphic pipeline
|
|
vk::PipelineLayout m_pipelineLayout;
|
|
vk::Pipeline m_graphicsPipeline;
|
|
std::vector<vk::DescriptorSetLayoutBinding> m_descSetLayoutBind;
|
|
vk::DescriptorPool m_descPool;
|
|
vk::DescriptorSetLayout m_descSetLayout;
|
|
vk::DescriptorSet m_descSet;
|
|
|
|
int m_maxFrames{10};
|
|
void resetFrame();
|
|
void updateFrame();
|
|
|
|
nvvkBuffer m_cameraMat; // Device-Host of the camera matrices
|
|
nvvkBuffer m_sceneDesc; // Device buffer of the OBJ instances
|
|
std::vector<nvvkTexture> m_textures; // vector of all textures of the scene
|
|
|
|
nvvkpp::DebugUtil m_debug; // Utility to name objects
|
|
|
|
nvvkAllocator m_alloc; // Allocator for buffer, images, acceleration structures
|
|
nvvkMemAllocator m_memAllocator;
|
|
|
|
|
|
// #Post
|
|
Offscreen m_offscreen;
|
|
void initOffscreen();
|
|
|
|
|
|
// #VKRay
|
|
Raytracer m_raytrace;
|
|
|
|
void initRayTracing();
|
|
void raytrace(const vk::CommandBuffer& cmdBuf, const nvmath::vec4f& clearColor);
|
|
|
|
// Implicit
|
|
ImplInst m_implObjects;
|
|
|
|
void addImplSphere(nvmath::vec3f center, float radius, int matId);
|
|
void addImplCube(nvmath::vec3f minumum, nvmath::vec3f maximum, int matId);
|
|
void addImplMaterial(const MaterialObj& mat);
|
|
void createImplictBuffers();
|
|
};
|