2019-07-25 11:52:09 +00:00
|
|
|
// Copyright(c) 2019, 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.
|
|
|
|
//
|
|
|
|
// VulkanHpp Samples : RayTracing
|
|
|
|
// Simple sample how to ray trace using Vulkan
|
|
|
|
|
2020-09-30 10:00:32 +00:00
|
|
|
#if defined( _MSC_VER )
|
|
|
|
# pragma warning( disable : 4201 ) // disable warning C4201: nonstandard extension used: nameless struct/union; needed
|
|
|
|
// to get glm/detail/type_vec?.hpp without warnings
|
|
|
|
#elif defined( __clang__ )
|
|
|
|
# pragma clang diagnostic ignored "-Wmissing-braces"
|
|
|
|
# if ( 10 <= __clang_major__ )
|
|
|
|
# pragma clang diagnostic ignored "-Wdeprecated-volatile" // to keep glm/detail/type_half.inl compiling
|
|
|
|
# endif
|
|
|
|
#elif defined( __GNUC__ )
|
|
|
|
#else
|
|
|
|
// unknow compiler... just ignore the warnings for yourselves ;)
|
|
|
|
#endif
|
|
|
|
|
2020-07-08 08:58:37 +00:00
|
|
|
// clang-format off
|
|
|
|
// we need to include vulkan.hpp before glfw3.h, so stop clang-format to reorder them
|
2020-04-12 19:49:12 +00:00
|
|
|
#include <vulkan/vulkan.hpp>
|
|
|
|
#include <GLFW/glfw3.h>
|
2020-07-08 08:58:37 +00:00
|
|
|
// clang-format on
|
2019-07-25 11:52:09 +00:00
|
|
|
#include <numeric>
|
|
|
|
#include <random>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
|
|
|
|
#define GLM_FORCE_RADIANS
|
|
|
|
#define GLM_ENABLE_EXPERIMENTAL
|
2020-01-28 09:16:10 +00:00
|
|
|
#include "../utils/shaders.hpp"
|
|
|
|
#include "../utils/utils.hpp"
|
2020-04-12 19:49:12 +00:00
|
|
|
#include "CameraManipulator.hpp"
|
2019-07-25 11:52:09 +00:00
|
|
|
#include "SPIRV/GlslangToSpv.h"
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
#include <glm/glm.hpp>
|
|
|
|
#include <glm/gtc/matrix_inverse.hpp>
|
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
|
|
|
|
static char const * AppName = "RayTracing";
|
|
|
|
static char const * EngineName = "Vulkan.hpp";
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
struct GeometryInstanceData
|
|
|
|
{
|
2022-02-28 09:11:04 +00:00
|
|
|
GeometryInstanceData(
|
|
|
|
glm::mat4x4 const & transform_, uint32_t instanceID_, uint8_t mask_, uint32_t instanceOffset_, uint8_t flags_, uint64_t accelerationStructureHandle_ )
|
|
|
|
: instanceId( instanceID_ ), mask( mask_ ), instanceOffset( instanceOffset_ ), flags( flags_ ), accelerationStructureHandle( accelerationStructureHandle_ )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
assert( !( instanceID_ & 0xFF000000 ) && !( instanceOffset_ & 0xFF000000 ) );
|
|
|
|
memcpy( transform, &transform_, 12 * sizeof( float ) );
|
2019-07-25 11:52:09 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
float transform[12]; // Transform matrix, containing only the top 3 rows
|
2022-03-23 16:08:19 +00:00
|
|
|
uint32_t instanceId : 24; // Instance index
|
|
|
|
uint32_t mask : 8; // Visibility mask
|
2020-04-12 19:49:12 +00:00
|
|
|
uint32_t instanceOffset : 24; // Index of the hit group which will be invoked when a ray hits the instance
|
2022-03-23 16:08:19 +00:00
|
|
|
uint32_t flags : 8; // Instance flags, such as culling
|
2020-04-12 19:49:12 +00:00
|
|
|
uint64_t accelerationStructureHandle; // Opaque handle of the bottom-level acceleration structure
|
2019-07-25 11:52:09 +00:00
|
|
|
};
|
2020-04-12 19:49:12 +00:00
|
|
|
static_assert( sizeof( GeometryInstanceData ) == 64, "GeometryInstanceData structure compiles to incorrect size" );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
struct AccelerationStructureData
|
|
|
|
{
|
2021-02-17 09:49:59 +00:00
|
|
|
void clear( vk::Device device )
|
|
|
|
{
|
|
|
|
device.destroyAccelerationStructureNV( accelerationStructure );
|
|
|
|
if ( scratchBufferData )
|
|
|
|
{
|
|
|
|
scratchBufferData->clear( device );
|
|
|
|
}
|
|
|
|
if ( resultBufferData )
|
|
|
|
{
|
|
|
|
resultBufferData->clear( device );
|
|
|
|
}
|
|
|
|
if ( instanceBufferData )
|
|
|
|
{
|
|
|
|
instanceBufferData->clear( device );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
vk::AccelerationStructureNV accelerationStructure;
|
2019-09-26 07:55:15 +00:00
|
|
|
std::unique_ptr<vk::su::BufferData> scratchBufferData;
|
|
|
|
std::unique_ptr<vk::su::BufferData> resultBufferData;
|
|
|
|
std::unique_ptr<vk::su::BufferData> instanceBufferData;
|
2019-07-25 11:52:09 +00:00
|
|
|
};
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
AccelerationStructureData createAccelerationStructureData( vk::PhysicalDevice const & physicalDevice,
|
|
|
|
vk::Device const & device,
|
|
|
|
vk::CommandBuffer const & commandBuffer,
|
|
|
|
std::vector<std::pair<vk::AccelerationStructureNV, glm::mat4x4>> const & instances,
|
|
|
|
std::vector<vk::GeometryNV> const & geometries )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
assert( instances.empty() ^ geometries.empty() );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
AccelerationStructureData accelerationStructureData;
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::AccelerationStructureTypeNV accelerationStructureType =
|
|
|
|
instances.empty() ? vk::AccelerationStructureTypeNV::eBottomLevel : vk::AccelerationStructureTypeNV::eTopLevel;
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::AccelerationStructureInfoNV accelerationStructureInfo( accelerationStructureType, {}, vk::su::checked_cast<uint32_t>( instances.size() ), geometries );
|
2021-02-17 09:49:59 +00:00
|
|
|
accelerationStructureData.accelerationStructure =
|
|
|
|
device.createAccelerationStructureNV( vk::AccelerationStructureCreateInfoNV( 0, accelerationStructureInfo ) );
|
2020-04-12 19:49:12 +00:00
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::AccelerationStructureMemoryRequirementsInfoNV objectRequirements( vk::AccelerationStructureMemoryRequirementsTypeNV::eObject,
|
|
|
|
accelerationStructureData.accelerationStructure );
|
|
|
|
vk::DeviceSize resultSizeInBytes = device.getAccelerationStructureMemoryRequirementsNV( objectRequirements ).memoryRequirements.size;
|
2020-04-12 19:49:12 +00:00
|
|
|
assert( 0 < resultSizeInBytes );
|
2022-02-28 09:11:04 +00:00
|
|
|
accelerationStructureData.resultBufferData = std::unique_ptr<vk::su::BufferData>(
|
|
|
|
new vk::su::BufferData( physicalDevice, device, resultSizeInBytes, vk::BufferUsageFlagBits::eRayTracingNV, vk::MemoryPropertyFlagBits::eDeviceLocal ) );
|
|
|
|
|
|
|
|
vk::AccelerationStructureMemoryRequirementsInfoNV buildScratchRequirements( vk::AccelerationStructureMemoryRequirementsTypeNV::eBuildScratch,
|
|
|
|
accelerationStructureData.accelerationStructure );
|
|
|
|
vk::AccelerationStructureMemoryRequirementsInfoNV updateScratchRequirements( vk::AccelerationStructureMemoryRequirementsTypeNV::eUpdateScratch,
|
|
|
|
accelerationStructureData.accelerationStructure );
|
|
|
|
vk::DeviceSize scratchSizeInBytes = std::max( device.getAccelerationStructureMemoryRequirementsNV( buildScratchRequirements ).memoryRequirements.size,
|
|
|
|
device.getAccelerationStructureMemoryRequirementsNV( updateScratchRequirements ).memoryRequirements.size );
|
2020-04-12 19:49:12 +00:00
|
|
|
assert( 0 < scratchSizeInBytes );
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
accelerationStructureData.scratchBufferData = std::unique_ptr<vk::su::BufferData>(
|
|
|
|
new vk::su::BufferData( physicalDevice, device, scratchSizeInBytes, vk::BufferUsageFlagBits::eRayTracingNV, vk::MemoryPropertyFlagBits::eDeviceLocal ) );
|
2020-04-12 19:49:12 +00:00
|
|
|
|
|
|
|
if ( !instances.empty() )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
2022-02-28 09:11:04 +00:00
|
|
|
accelerationStructureData.instanceBufferData = std::unique_ptr<vk::su::BufferData>(
|
|
|
|
new vk::su::BufferData( physicalDevice, device, instances.size() * sizeof( GeometryInstanceData ), vk::BufferUsageFlagBits::eRayTracingNV ) );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
std::vector<GeometryInstanceData> geometryInstanceData;
|
2020-04-12 19:49:12 +00:00
|
|
|
for ( size_t i = 0; i < instances.size(); i++ )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
2021-02-17 09:49:59 +00:00
|
|
|
uint64_t accelerationStructureHandle = device.getAccelerationStructureHandleNV<uint64_t>( instances[i].first );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
// For each instance we set its instance index to its index i in the instance vector, and set
|
|
|
|
// its hit group index to 2*i. The hit group index defines which entry of the shader binding
|
|
|
|
// table will contain the hit group to be executed when hitting this instance. We set this
|
|
|
|
// index to 2*i due to the use of 2 types of rays in the scene: the camera rays and the shadow
|
|
|
|
// rays. For each instance, the SBT will then have 2 hit groups
|
2022-02-28 09:11:04 +00:00
|
|
|
geometryInstanceData.emplace_back( GeometryInstanceData( glm::transpose( instances[i].second ),
|
|
|
|
static_cast<uint32_t>( i ),
|
|
|
|
0xFF,
|
|
|
|
static_cast<uint32_t>( 2 * i ),
|
|
|
|
static_cast<uint8_t>( vk::GeometryInstanceFlagBitsNV::eTriangleCullDisable ),
|
|
|
|
accelerationStructureHandle ) );
|
2019-07-25 11:52:09 +00:00
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
accelerationStructureData.instanceBufferData->upload( device, geometryInstanceData );
|
2019-07-25 11:52:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
device.bindAccelerationStructureMemoryNV(
|
|
|
|
vk::BindAccelerationStructureMemoryInfoNV( accelerationStructureData.accelerationStructure, accelerationStructureData.resultBufferData->deviceMemory ) );
|
2020-04-12 19:49:12 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
commandBuffer.buildAccelerationStructureNV(
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::AccelerationStructureInfoNV( accelerationStructureType, {}, vk::su::checked_cast<uint32_t>( instances.size() ), geometries ),
|
2021-02-17 09:49:59 +00:00
|
|
|
accelerationStructureData.instanceBufferData ? accelerationStructureData.instanceBufferData->buffer : nullptr,
|
2020-04-12 19:49:12 +00:00
|
|
|
0,
|
|
|
|
false,
|
2021-02-17 09:49:59 +00:00
|
|
|
accelerationStructureData.accelerationStructure,
|
2020-04-12 19:49:12 +00:00
|
|
|
nullptr,
|
2021-02-17 09:49:59 +00:00
|
|
|
accelerationStructureData.scratchBufferData->buffer,
|
2020-04-12 19:49:12 +00:00
|
|
|
0 );
|
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
commandBuffer.pipelineBarrier( vk::PipelineStageFlagBits::eAccelerationStructureBuildNV,
|
|
|
|
vk::PipelineStageFlagBits::eAccelerationStructureBuildNV,
|
|
|
|
{},
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::MemoryBarrier( vk::AccessFlagBits::eAccelerationStructureWriteNV | vk::AccessFlagBits::eAccelerationStructureReadNV,
|
|
|
|
vk::AccessFlagBits::eAccelerationStructureWriteNV | vk::AccessFlagBits::eAccelerationStructureReadNV ),
|
2021-02-17 09:49:59 +00:00
|
|
|
{},
|
|
|
|
{} );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
return accelerationStructureData;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct PerFrameData
|
|
|
|
{
|
2021-02-17 09:49:59 +00:00
|
|
|
void clear( vk::Device device )
|
|
|
|
{
|
|
|
|
device.freeCommandBuffers( commandPool, commandBuffer );
|
|
|
|
device.destroyCommandPool( commandPool );
|
|
|
|
device.destroyFence( fence );
|
|
|
|
device.destroySemaphore( presentCompleteSemaphore );
|
|
|
|
device.destroySemaphore( renderCompleteSemaphore );
|
|
|
|
}
|
|
|
|
|
|
|
|
vk::CommandPool commandPool;
|
|
|
|
vk::CommandBuffer commandBuffer;
|
|
|
|
vk::Fence fence;
|
|
|
|
vk::Semaphore presentCompleteSemaphore;
|
|
|
|
vk::Semaphore renderCompleteSemaphore;
|
2019-07-25 11:52:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct UniformBufferObject
|
|
|
|
{
|
|
|
|
glm::mat4 model;
|
|
|
|
glm::mat4 view;
|
|
|
|
glm::mat4 proj;
|
|
|
|
glm::mat4 modelIT;
|
|
|
|
glm::mat4 viewInverse;
|
|
|
|
glm::mat4 projInverse;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Material
|
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
glm::vec3 diffuse = glm::vec3( 0.7f, 0.7f, 0.7f );
|
|
|
|
int textureID = -1;
|
2019-07-25 11:52:09 +00:00
|
|
|
};
|
2020-04-12 19:49:12 +00:00
|
|
|
const size_t MaterialStride = ( ( sizeof( Material ) + 15 ) / 16 ) * 16;
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
struct Vertex
|
|
|
|
{
|
2022-02-28 09:11:04 +00:00
|
|
|
Vertex( glm::vec3 const & p, glm::vec3 const & n, glm::vec2 const & tc, int m = 0 ) : pos( p ), nrm( n ), texCoord( tc ), matID( m ) {}
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
glm::vec3 pos;
|
|
|
|
glm::vec3 nrm;
|
|
|
|
glm::vec2 texCoord;
|
|
|
|
int matID;
|
|
|
|
};
|
2020-04-12 19:49:12 +00:00
|
|
|
const size_t VertexStride = ( ( sizeof( Vertex ) + 15 ) / 16 ) * 16;
|
2019-07-25 11:52:09 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
static const std::vector<Vertex> cubeData = {
|
2019-07-25 11:52:09 +00:00
|
|
|
// pos nrm texcoord matID
|
|
|
|
// front face
|
2020-04-12 19:49:12 +00:00
|
|
|
{ Vertex( glm::vec3( -1.0f, -1.0f, 1.0f ), glm::vec3( 0.0f, 0.0f, 1.0f ), glm::vec2( 0.0f, 0.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( 1.0f, -1.0f, 1.0f ), glm::vec3( 0.0f, 0.0f, 1.0f ), glm::vec2( 1.0f, 0.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( 1.0f, 1.0f, 1.0f ), glm::vec3( 0.0f, 0.0f, 1.0f ), glm::vec2( 1.0f, 1.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( 1.0f, 1.0f, 1.0f ), glm::vec3( 0.0f, 0.0f, 1.0f ), glm::vec2( 1.0f, 1.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( -1.0f, 1.0f, 1.0f ), glm::vec3( 0.0f, 0.0f, 1.0f ), glm::vec2( 0.0f, 1.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( -1.0f, -1.0f, 1.0f ), glm::vec3( 0.0f, 0.0f, 1.0f ), glm::vec2( 0.0f, 0.0f ), 0 ) },
|
2019-07-25 11:52:09 +00:00
|
|
|
// back face
|
2020-04-12 19:49:12 +00:00
|
|
|
{ Vertex( glm::vec3( 1.0f, -1.0f, -1.0f ), glm::vec3( 0.0f, 0.0f, -1.0f ), glm::vec2( 0.0f, 0.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( -1.0f, -1.0f, -1.0f ), glm::vec3( 0.0f, 0.0f, -1.0f ), glm::vec2( 1.0f, 0.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( -1.0f, 1.0f, -1.0f ), glm::vec3( 0.0f, 0.0f, -1.0f ), glm::vec2( 1.0f, 1.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( -1.0f, 1.0f, -1.0f ), glm::vec3( 0.0f, 0.0f, -1.0f ), glm::vec2( 1.0f, 1.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( 1.0f, 1.0f, -1.0f ), glm::vec3( 0.0f, 0.0f, -1.0f ), glm::vec2( 0.0f, 1.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( 1.0f, -1.0f, -1.0f ), glm::vec3( 0.0f, 0.0f, -1.0f ), glm::vec2( 0.0f, 0.0f ), 0 ) },
|
2019-07-25 11:52:09 +00:00
|
|
|
// left face
|
2020-04-12 19:49:12 +00:00
|
|
|
{ Vertex( glm::vec3( -1.0f, -1.0f, -1.0f ), glm::vec3( -1.0f, 0.0f, 0.0f ), glm::vec2( 0.0f, 0.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( -1.0f, -1.0f, 1.0f ), glm::vec3( -1.0f, 0.0f, 0.0f ), glm::vec2( 1.0f, 0.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( -1.0f, 1.0f, 1.0f ), glm::vec3( -1.0f, 0.0f, 0.0f ), glm::vec2( 1.0f, 1.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( -1.0f, 1.0f, 1.0f ), glm::vec3( -1.0f, 0.0f, 0.0f ), glm::vec2( 1.0f, 1.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( -1.0f, 1.0f, -1.0f ), glm::vec3( -1.0f, 0.0f, 0.0f ), glm::vec2( 0.0f, 1.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( -1.0f, -1.0f, -1.0f ), glm::vec3( -1.0f, 0.0f, 0.0f ), glm::vec2( 0.0f, 0.0f ), 0 ) },
|
2019-07-25 11:52:09 +00:00
|
|
|
// right face
|
2020-04-12 19:49:12 +00:00
|
|
|
{ Vertex( glm::vec3( 1.0f, -1.0f, 1.0f ), glm::vec3( 1.0f, 0.0f, 0.0f ), glm::vec2( 0.0f, 0.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( 1.0f, -1.0f, -1.0f ), glm::vec3( 1.0f, 0.0f, 0.0f ), glm::vec2( 1.0f, 0.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( 1.0f, 1.0f, -1.0f ), glm::vec3( 1.0f, 0.0f, 0.0f ), glm::vec2( 1.0f, 1.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( 1.0f, 1.0f, -1.0f ), glm::vec3( 1.0f, 0.0f, 0.0f ), glm::vec2( 1.0f, 1.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( 1.0f, 1.0f, 1.0f ), glm::vec3( 1.0f, 0.0f, 0.0f ), glm::vec2( 0.0f, 1.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( 1.0f, -1.0f, 1.0f ), glm::vec3( 1.0f, 0.0f, 0.0f ), glm::vec2( 0.0f, 0.0f ), 0 ) },
|
2019-07-25 11:52:09 +00:00
|
|
|
// top face
|
2020-04-12 19:49:12 +00:00
|
|
|
{ Vertex( glm::vec3( -1.0f, 1.0f, 1.0f ), glm::vec3( 0.0f, 1.0f, 0.0f ), glm::vec2( 0.0f, 0.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( 1.0f, 1.0f, 1.0f ), glm::vec3( 0.0f, 1.0f, 0.0f ), glm::vec2( 1.0f, 0.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( 1.0f, 1.0f, -1.0f ), glm::vec3( 0.0f, 1.0f, 0.0f ), glm::vec2( 1.0f, 1.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( 1.0f, 1.0f, -1.0f ), glm::vec3( 0.0f, 1.0f, 0.0f ), glm::vec2( 1.0f, 1.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( -1.0f, 1.0f, -1.0f ), glm::vec3( 0.0f, 1.0f, 0.0f ), glm::vec2( 0.0f, 1.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( -1.0f, 1.0f, 1.0f ), glm::vec3( 0.0f, 1.0f, 0.0f ), glm::vec2( 0.0f, 0.0f ), 0 ) },
|
2019-07-25 11:52:09 +00:00
|
|
|
// bottom face
|
2020-04-12 19:49:12 +00:00
|
|
|
{ Vertex( glm::vec3( -1.0f, -1.0f, -1.0f ), glm::vec3( 0.0f, -1.0f, 0.0f ), glm::vec2( 0.0f, 0.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( 1.0f, -1.0f, -1.0f ), glm::vec3( 0.0f, -1.0f, 0.0f ), glm::vec2( 1.0f, 0.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( 1.0f, -1.0f, 1.0f ), glm::vec3( 0.0f, -1.0f, 0.0f ), glm::vec2( 1.0f, 1.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( 1.0f, -1.0f, 1.0f ), glm::vec3( 0.0f, -1.0f, 0.0f ), glm::vec2( 1.0f, 1.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( -1.0f, -1.0f, 1.0f ), glm::vec3( 0.0f, -1.0f, 0.0f ), glm::vec2( 0.0f, 1.0f ), 0 ) },
|
|
|
|
{ Vertex( glm::vec3( -1.0f, -1.0f, -1.0f ), glm::vec3( 0.0f, -1.0f, 0.0f ), glm::vec2( 0.0f, 0.0f ), 0 ) },
|
2019-07-25 11:52:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static std::string vertexShaderText = R"(
|
|
|
|
#version 450
|
|
|
|
|
|
|
|
#extension GL_ARB_separate_shader_objects : enable
|
|
|
|
|
|
|
|
layout(binding = 0) uniform UniformBufferObject
|
|
|
|
{
|
|
|
|
mat4 model;
|
|
|
|
mat4 view;
|
|
|
|
mat4 proj;
|
|
|
|
mat4 modelIT;
|
|
|
|
} ubo;
|
|
|
|
|
|
|
|
layout(location = 0) in vec3 inPosition;
|
|
|
|
layout(location = 1) in vec3 inNormal;
|
|
|
|
layout(location = 2) in vec2 inTexCoord;
|
|
|
|
layout(location = 3) in int inMatID;
|
|
|
|
|
|
|
|
layout(location = 0) flat out int outMatID;
|
|
|
|
layout(location = 1) out vec2 outTexCoord;
|
|
|
|
layout(location = 2) out vec3 outNormal;
|
|
|
|
|
|
|
|
out gl_PerVertex
|
|
|
|
{
|
|
|
|
vec4 gl_Position;
|
|
|
|
};
|
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
|
|
|
gl_Position = ubo.proj * ubo.view * ubo.model * vec4(inPosition, 1.0);
|
|
|
|
outMatID = inMatID;
|
|
|
|
outTexCoord = inTexCoord;
|
|
|
|
outNormal = vec3(ubo.modelIT * vec4(inNormal, 0.0));
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
|
|
|
|
static std::string fragmentShaderText = R"(
|
|
|
|
#version 450
|
|
|
|
|
|
|
|
#extension GL_ARB_separate_shader_objects : enable
|
|
|
|
#extension GL_EXT_nonuniform_qualifier : enable
|
|
|
|
|
|
|
|
layout(location = 0) flat in int matIndex;
|
|
|
|
layout(location = 1) in vec2 texCoord;
|
|
|
|
layout(location = 2) in vec3 normal;
|
|
|
|
|
|
|
|
struct Material
|
|
|
|
{
|
|
|
|
vec3 diffuse;
|
|
|
|
int textureID;
|
|
|
|
};
|
|
|
|
const int sizeofMat = 1;
|
|
|
|
|
|
|
|
layout(binding = 1) buffer MaterialBufferObject { vec4[] m; } materials;
|
|
|
|
layout(binding = 2) uniform sampler2D[] textureSamplers;
|
|
|
|
|
|
|
|
Material unpackMaterial()
|
|
|
|
{
|
|
|
|
Material m;
|
|
|
|
vec4 d0 = materials.m[sizeofMat * matIndex + 0];
|
|
|
|
|
|
|
|
m.diffuse = d0.xyz;
|
|
|
|
m.textureID = floatBitsToInt(d0.w);
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
layout(location = 0) out vec4 outColor;
|
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
|
|
|
vec3 lightVector = normalize(vec3(5, 4, 3));
|
|
|
|
|
|
|
|
float dot_product = max(dot(lightVector, normalize(normal)), 0.2);
|
|
|
|
|
|
|
|
Material m = unpackMaterial();
|
|
|
|
vec3 c = m.diffuse;
|
|
|
|
if (0 <= m.textureID)
|
|
|
|
{
|
|
|
|
c *= texture(textureSamplers[m.textureID], texCoord).xyz;
|
|
|
|
}
|
|
|
|
c *= dot_product;
|
|
|
|
|
|
|
|
outColor = vec4(c, 1);
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
|
|
|
|
static std::string raygenShaderText = R"(
|
|
|
|
#version 460
|
|
|
|
|
|
|
|
#extension GL_NV_ray_tracing : require
|
|
|
|
|
|
|
|
layout(binding = 0, set = 0) uniform accelerationStructureNV topLevelAS;
|
|
|
|
layout(binding = 1, set = 0, rgba8) uniform image2D image;
|
|
|
|
|
|
|
|
layout(binding=2, set = 0) uniform UniformBufferObject
|
|
|
|
{
|
|
|
|
mat4 model;
|
|
|
|
mat4 view;
|
|
|
|
mat4 proj;
|
|
|
|
mat4 modelIT;
|
|
|
|
mat4 viewInverse;
|
|
|
|
mat4 projInverse;
|
|
|
|
} cam;
|
|
|
|
|
|
|
|
layout(location = 0) rayPayloadNV vec3 hitValue;
|
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
|
|
|
const vec2 pixelCenter = vec2(gl_LaunchIDNV.xy) + vec2(0.5);
|
|
|
|
const vec2 inUV = pixelCenter/vec2(gl_LaunchSizeNV.xy);
|
|
|
|
vec2 d = inUV * 2.0 - 1.0;
|
|
|
|
|
|
|
|
vec4 origin = cam.viewInverse*vec4(0,0,0,1);
|
|
|
|
vec4 target = cam.projInverse * vec4(d.x, d.y, 1, 1) ;
|
|
|
|
vec4 direction = cam.viewInverse*vec4(normalize(target.xyz), 0) ;
|
|
|
|
|
|
|
|
uint rayFlags = gl_RayFlagsOpaqueNV;
|
|
|
|
uint cullMask = 0xff;
|
|
|
|
float tmin = 0.001;
|
|
|
|
float tmax = 10000.0;
|
|
|
|
|
|
|
|
traceNV(topLevelAS, rayFlags, cullMask, 0 /*sbtRecordOffset*/, 0 /*sbtRecordStride*/, 0 /*missIndex*/, origin.xyz, tmin, direction.xyz, tmax, 0 /*payload*/);
|
|
|
|
imageStore(image, ivec2(gl_LaunchIDNV.xy), vec4(hitValue, 0.0));
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
|
|
|
|
static std::string missShaderText = R"(
|
|
|
|
#version 460
|
|
|
|
|
|
|
|
#extension GL_NV_ray_tracing : require
|
|
|
|
|
|
|
|
layout(location = 0) rayPayloadInNV vec3 hitValue;
|
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
|
|
|
hitValue = vec3(0.0, 0.1, 0.3);
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
|
|
|
|
static std::string shadowMissShaderText = R"(
|
|
|
|
#version 460
|
|
|
|
|
|
|
|
#extension GL_NV_ray_tracing : require
|
|
|
|
|
|
|
|
layout(location = 2) rayPayloadInNV bool isShadowed;
|
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
|
|
|
isShadowed = false;
|
|
|
|
})";
|
|
|
|
|
|
|
|
static std::string closestHitShaderText = R"(
|
|
|
|
#version 460
|
|
|
|
|
|
|
|
#extension GL_NV_ray_tracing : require
|
|
|
|
#extension GL_EXT_nonuniform_qualifier : enable
|
|
|
|
|
|
|
|
layout(location = 0) rayPayloadInNV vec3 hitValue;
|
|
|
|
layout(location = 2) rayPayloadNV bool isShadowed;
|
|
|
|
|
|
|
|
hitAttributeNV vec3 attribs;
|
|
|
|
layout(binding = 0, set = 0) uniform accelerationStructureNV topLevelAS;
|
|
|
|
|
|
|
|
layout(binding = 3, set = 0) buffer Vertices { vec4 v[]; } vertices;
|
|
|
|
layout(binding = 4, set = 0) buffer Indices { uint i[]; } indices;
|
|
|
|
|
|
|
|
layout(binding = 5, set = 0) buffer MatColorBufferObject { vec4[] m; } materials;
|
|
|
|
layout(binding = 6, set = 0) uniform sampler2D[] textureSamplers;
|
|
|
|
|
|
|
|
struct Vertex
|
|
|
|
{
|
|
|
|
vec3 pos;
|
|
|
|
vec3 nrm;
|
|
|
|
vec2 texCoord;
|
|
|
|
int matIndex;
|
|
|
|
};
|
|
|
|
// Number of vec4 values used to represent a vertex
|
|
|
|
uint vertexSize = 3;
|
|
|
|
|
|
|
|
Vertex unpackVertex(uint index)
|
|
|
|
{
|
|
|
|
Vertex v;
|
|
|
|
|
|
|
|
vec4 d0 = vertices.v[vertexSize * index + 0];
|
|
|
|
vec4 d1 = vertices.v[vertexSize * index + 1];
|
|
|
|
vec4 d2 = vertices.v[vertexSize * index + 2];
|
|
|
|
|
|
|
|
v.pos = d0.xyz;
|
|
|
|
v.nrm = vec3(d0.w, d1.xy);
|
|
|
|
v.texCoord = d1.zw;
|
|
|
|
v.matIndex = floatBitsToInt(d2.x);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Material
|
|
|
|
{
|
|
|
|
vec3 diffuse;
|
|
|
|
int textureID;
|
|
|
|
};
|
|
|
|
// Number of vec4 values used to represent a material
|
|
|
|
const int sizeofMat = 1;
|
|
|
|
|
|
|
|
Material unpackMaterial(int matIndex)
|
|
|
|
{
|
|
|
|
Material m;
|
|
|
|
vec4 d0 = materials.m[sizeofMat * matIndex + 0];
|
|
|
|
|
|
|
|
m.diffuse = d0.xyz;
|
|
|
|
m.textureID = floatBitsToInt(d0.w);
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
void main()
|
|
|
|
{
|
|
|
|
ivec3 ind = ivec3(indices.i[3 * gl_PrimitiveID], indices.i[3 * gl_PrimitiveID + 1], indices.i[3 * gl_PrimitiveID + 2]);
|
|
|
|
|
|
|
|
Vertex v0 = unpackVertex(ind.x);
|
|
|
|
Vertex v1 = unpackVertex(ind.y);
|
|
|
|
Vertex v2 = unpackVertex(ind.z);
|
|
|
|
|
|
|
|
const vec3 barycentrics = vec3(1.0 - attribs.x - attribs.y, attribs.x, attribs.y);
|
|
|
|
|
|
|
|
vec3 normal = normalize(v0.nrm * barycentrics.x + v1.nrm * barycentrics.y + v2.nrm * barycentrics.z);
|
|
|
|
|
|
|
|
vec3 lightVector = normalize(vec3(5, 4, 3));
|
|
|
|
|
|
|
|
float dot_product = max(dot(lightVector, normal), 0.2);
|
|
|
|
|
|
|
|
Material mat = unpackMaterial(v1.matIndex);
|
|
|
|
|
|
|
|
vec3 c = dot_product * mat.diffuse;
|
|
|
|
if (0 <= mat.textureID)
|
|
|
|
{
|
|
|
|
vec2 texCoord = v0.texCoord * barycentrics.x + v1.texCoord * barycentrics.y + v2.texCoord * barycentrics.z;
|
|
|
|
c *= texture(textureSamplers[mat.textureID], texCoord).xyz;
|
|
|
|
}
|
|
|
|
float tmin = 0.001;
|
|
|
|
float tmax = 100.0;
|
|
|
|
vec3 origin = gl_WorldRayOriginNV + gl_WorldRayDirectionNV * gl_HitTNV;
|
|
|
|
isShadowed = true;
|
|
|
|
traceNV(topLevelAS, gl_RayFlagsTerminateOnFirstHitNV|gl_RayFlagsOpaqueNV|gl_RayFlagsSkipClosestHitShaderNV, 0xFF, 1 /* sbtRecordOffset */, 0 /* sbtRecordStride */, 1 /* missIndex */, origin,
|
|
|
|
tmin, lightVector, tmax, 2 /*payload location*/);
|
|
|
|
hitValue = c;
|
|
|
|
if (isShadowed)
|
|
|
|
{
|
|
|
|
hitValue *= 0.3f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
|
|
|
|
#ifndef IMGUI_VK_QUEUED_FRAMES
|
2020-04-12 19:49:12 +00:00
|
|
|
# define IMGUI_VK_QUEUED_FRAMES 2
|
2019-07-25 11:52:09 +00:00
|
|
|
#endif // !IMGUI_VK_QUEUED_FRAMES
|
|
|
|
|
|
|
|
struct AppInfo
|
|
|
|
{
|
|
|
|
vk::su::CameraManipulator cameraManipulator;
|
2020-04-12 19:49:12 +00:00
|
|
|
bool useRasterRender = false;
|
2019-07-25 11:52:09 +00:00
|
|
|
};
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
static void check_vk_result( VkResult err )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( err != 0 )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
std::cerr << AppName << ": Vulkan error " << vk::to_string( static_cast<vk::Result>( err ) );
|
|
|
|
if ( err < 0 )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
static void cursorPosCallback( GLFWwindow * window, double mouseX, double mouseY )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::su::CameraManipulator::MouseButton mouseButton =
|
2022-02-28 09:11:04 +00:00
|
|
|
( glfwGetMouseButton( window, GLFW_MOUSE_BUTTON_LEFT ) == GLFW_PRESS ) ? vk::su::CameraManipulator::MouseButton::Left
|
|
|
|
: ( glfwGetMouseButton( window, GLFW_MOUSE_BUTTON_MIDDLE ) == GLFW_PRESS ) ? vk::su::CameraManipulator::MouseButton::Middle
|
|
|
|
: ( glfwGetMouseButton( window, GLFW_MOUSE_BUTTON_RIGHT ) == GLFW_PRESS ) ? vk::su::CameraManipulator::MouseButton::Right
|
|
|
|
: vk::su::CameraManipulator::MouseButton::None;
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( mouseButton != vk::su::CameraManipulator::MouseButton::None )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
|
|
|
vk::su::CameraManipulator::ModifierFlags modifiers;
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( glfwGetKey( window, GLFW_KEY_LEFT_ALT ) == GLFW_PRESS )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
|
|
|
modifiers |= vk::su::CameraManipulator::ModifierFlagBits::Alt;
|
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( glfwGetKey( window, GLFW_KEY_LEFT_CONTROL ) == GLFW_PRESS )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
|
|
|
modifiers |= vk::su::CameraManipulator::ModifierFlagBits::Ctrl;
|
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( glfwGetKey( window, GLFW_KEY_LEFT_SHIFT ) == GLFW_PRESS )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
|
|
|
modifiers |= vk::su::CameraManipulator::ModifierFlagBits::Shift;
|
|
|
|
}
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::su::CameraManipulator & cameraManipulator = reinterpret_cast<AppInfo *>( glfwGetWindowUserPointer( window ) )->cameraManipulator;
|
|
|
|
cameraManipulator.mouseMove( glm::ivec2( static_cast<int>( mouseX ), static_cast<int>( mouseY ) ), mouseButton, modifiers );
|
2019-07-25 11:52:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
static void errorCallback( int error, const char * description )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
fprintf( stderr, "GLFW Error %d: %s\n", error, description );
|
2019-07-25 11:52:09 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
static void framebufferSizeCallback( GLFWwindow * window, int w, int h )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::su::CameraManipulator & cameraManipulator = reinterpret_cast<AppInfo *>( glfwGetWindowUserPointer( window ) )->cameraManipulator;
|
2020-04-12 19:49:12 +00:00
|
|
|
cameraManipulator.setWindowSize( glm::ivec2( w, h ) );
|
2019-07-25 11:52:09 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
static void keyCallback( GLFWwindow * window, int key, int /*scancode*/, int action, int /*mods*/ )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( action == GLFW_PRESS )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
switch ( key )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
|
|
|
case GLFW_KEY_ESCAPE:
|
2020-04-12 19:49:12 +00:00
|
|
|
case 'Q': glfwSetWindowShouldClose( window, 1 ); break;
|
2019-07-25 11:52:09 +00:00
|
|
|
case 'R':
|
2022-02-28 09:11:04 +00:00
|
|
|
{
|
|
|
|
AppInfo * appInfo = reinterpret_cast<AppInfo *>( glfwGetWindowUserPointer( window ) );
|
|
|
|
appInfo->useRasterRender = !appInfo->useRasterRender;
|
|
|
|
}
|
|
|
|
break;
|
2019-07-25 11:52:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
static void mouseButtonCallback( GLFWwindow * window, int /*button*/, int /*action*/, int /*mods*/ )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
|
|
|
double xpos, ypos;
|
2020-04-12 19:49:12 +00:00
|
|
|
glfwGetCursorPos( window, &xpos, &ypos );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::su::CameraManipulator & cameraManipulator = reinterpret_cast<AppInfo *>( glfwGetWindowUserPointer( window ) )->cameraManipulator;
|
2020-04-12 19:49:12 +00:00
|
|
|
cameraManipulator.setMousePosition( glm::ivec2( static_cast<int>( xpos ), static_cast<int>( ypos ) ) );
|
2019-07-25 11:52:09 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
static void scrollCallback( GLFWwindow * window, double /*xoffset*/, double yoffset )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::su::CameraManipulator & cameraManipulator = reinterpret_cast<AppInfo *>( glfwGetWindowUserPointer( window ) )->cameraManipulator;
|
2020-04-12 19:49:12 +00:00
|
|
|
cameraManipulator.wheel( static_cast<int>( yoffset ) );
|
2019-07-25 11:52:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// random data and functions
|
|
|
|
static std::random_device randomDevice;
|
2020-04-12 19:49:12 +00:00
|
|
|
static std::mt19937 randomGenerator( randomDevice() );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
template <typename T>
|
2020-04-12 19:49:12 +00:00
|
|
|
T random( T minValue = std::numeric_limits<T>::min(), T maxValue = std::numeric_limits<T>::max() )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
static_assert( std::numeric_limits<T>::is_integer, "Type T needs to be an integral type!\n" );
|
|
|
|
std::uniform_int_distribution<> randomDistribution( minValue, maxValue );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
return static_cast<T>( randomDistribution( randomGenerator ) );
|
2019-07-25 11:52:09 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
glm::vec3 randomVec3( float minValue, float maxValue )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
std::uniform_real_distribution<float> randomDistribution( minValue, maxValue );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
return glm::vec3( randomDistribution( randomGenerator ), randomDistribution( randomGenerator ), randomDistribution( randomGenerator ) );
|
2019-07-25 11:52:09 +00:00
|
|
|
}
|
|
|
|
|
2020-09-30 10:00:32 +00:00
|
|
|
uint32_t roundUp( uint32_t value, uint32_t alignment )
|
2020-08-12 09:38:42 +00:00
|
|
|
{
|
|
|
|
return ( ( value + alignment - 1 ) / alignment ) * alignment;
|
|
|
|
}
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
int main( int /*argc*/, char ** /*argv*/ )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
|
|
|
// number of cubes in x-, y-, and z-direction
|
|
|
|
const size_t xMax = 10;
|
|
|
|
const size_t yMax = 10;
|
|
|
|
const size_t zMax = 10;
|
|
|
|
|
|
|
|
AppInfo appInfo;
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
// Setup glfw
|
2020-04-12 19:49:12 +00:00
|
|
|
glfwSetErrorCallback( errorCallback );
|
|
|
|
if ( !glfwInit() )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
|
|
|
std::cerr << AppName << ": can't initialize glfw!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( !glfwVulkanSupported() )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
|
|
|
std::cerr << AppName << ": Vulkan not supported!\n";
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a window using glfw
|
2020-04-12 19:49:12 +00:00
|
|
|
glfwWindowHint( GLFW_CLIENT_API, GLFW_NO_API );
|
|
|
|
vk::Extent2D windowExtent( 1280, 720 );
|
|
|
|
GLFWwindow * window = glfwCreateWindow( windowExtent.width, windowExtent.height, AppName, nullptr, nullptr );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
// install some callbacks
|
2020-04-12 19:49:12 +00:00
|
|
|
glfwSetCursorPosCallback( window, cursorPosCallback );
|
|
|
|
glfwSetFramebufferSizeCallback( window, framebufferSizeCallback );
|
|
|
|
glfwSetKeyCallback( window, keyCallback );
|
|
|
|
glfwSetMouseButtonCallback( window, mouseButtonCallback );
|
|
|
|
glfwSetScrollCallback( window, scrollCallback );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
// Setup camera and make it available as the userPointer in the glfw window
|
2020-04-12 19:49:12 +00:00
|
|
|
appInfo.cameraManipulator.setWindowSize( glm::u32vec2( windowExtent.width, windowExtent.height ) );
|
2022-02-28 09:11:04 +00:00
|
|
|
glm::vec3 diagonal = 3.0f * glm::vec3( static_cast<float>( xMax ), static_cast<float>( yMax ), static_cast<float>( zMax ) );
|
2020-04-12 19:49:12 +00:00
|
|
|
appInfo.cameraManipulator.setLookat( 1.5f * diagonal, 0.5f * diagonal, glm::vec3( 0, 1, 0 ) );
|
|
|
|
glfwSetWindowUserPointer( window, &appInfo );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
// Create Vulkan Instance with needed extensions
|
2020-04-12 19:49:12 +00:00
|
|
|
uint32_t glfwExtensionsCount;
|
|
|
|
const char ** glfwExtensions = glfwGetRequiredInstanceExtensions( &glfwExtensionsCount );
|
2019-07-25 11:52:09 +00:00
|
|
|
std::vector<std::string> instanceExtensions;
|
2020-04-12 19:49:12 +00:00
|
|
|
instanceExtensions.reserve( glfwExtensionsCount + 1 );
|
|
|
|
for ( uint32_t i = 0; i < glfwExtensionsCount; i++ )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
instanceExtensions.push_back( glfwExtensions[i] );
|
2019-07-25 11:52:09 +00:00
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
instanceExtensions.push_back( VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::Instance instance = vk::su::createInstance( AppName, EngineName, {}, instanceExtensions );
|
2020-04-12 19:49:12 +00:00
|
|
|
#if !defined( NDEBUG )
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::DebugUtilsMessengerEXT debugUtilsMessenger = instance.createDebugUtilsMessengerEXT( vk::su::makeDebugUtilsMessengerCreateInfoEXT() );
|
2019-07-25 11:52:09 +00:00
|
|
|
#endif
|
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::PhysicalDevice physicalDevice = instance.enumeratePhysicalDevices().front();
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
// Create Window Surface (using glfw)
|
|
|
|
vk::SurfaceKHR surface;
|
2022-02-28 09:11:04 +00:00
|
|
|
VkResult err = glfwCreateWindowSurface( static_cast<VkInstance>( instance ), window, nullptr, reinterpret_cast<VkSurfaceKHR *>( &surface ) );
|
2020-04-12 19:49:12 +00:00
|
|
|
check_vk_result( err );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex = vk::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, surface );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
// Create a Device with ray tracing support (besides some other extensions needed) and needed features
|
2022-02-28 09:11:04 +00:00
|
|
|
auto supportedFeatures = physicalDevice.getFeatures2<vk::PhysicalDeviceFeatures2, vk::PhysicalDeviceDescriptorIndexingFeaturesEXT>();
|
2022-05-02 07:43:45 +00:00
|
|
|
vk::Device device = vk::su::createDevice( physicalDevice,
|
|
|
|
graphicsAndPresentQueueFamilyIndex.first,
|
|
|
|
{ VK_EXT_DESCRIPTOR_INDEXING_EXTENSION_NAME,
|
|
|
|
VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME,
|
|
|
|
VK_KHR_MAINTENANCE_3_EXTENSION_NAME,
|
|
|
|
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
|
|
|
|
VK_NV_RAY_TRACING_EXTENSION_NAME },
|
|
|
|
&supportedFeatures.get<vk::PhysicalDeviceFeatures2>().features,
|
|
|
|
&supportedFeatures.get<vk::PhysicalDeviceDescriptorIndexingFeaturesEXT>() );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
// setup stuff per frame
|
|
|
|
std::array<PerFrameData, IMGUI_VK_QUEUED_FRAMES> perFrameData;
|
2020-04-12 19:49:12 +00:00
|
|
|
for ( int i = 0; i < IMGUI_VK_QUEUED_FRAMES; i++ )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
2022-02-28 09:11:04 +00:00
|
|
|
perFrameData[i].commandPool =
|
|
|
|
device.createCommandPool( vk::CommandPoolCreateInfo( vk::CommandPoolCreateFlagBits::eResetCommandBuffer, graphicsAndPresentQueueFamilyIndex.first ) );
|
|
|
|
perFrameData[i].commandBuffer =
|
|
|
|
device.allocateCommandBuffers( vk::CommandBufferAllocateInfo( perFrameData[i].commandPool, vk::CommandBufferLevel::ePrimary, 1 ) ).front();
|
|
|
|
perFrameData[i].fence = device.createFence( vk::FenceCreateInfo( vk::FenceCreateFlagBits::eSignaled ) );
|
2021-02-17 09:49:59 +00:00
|
|
|
perFrameData[i].presentCompleteSemaphore = device.createSemaphore( vk::SemaphoreCreateInfo() );
|
|
|
|
perFrameData[i].renderCompleteSemaphore = device.createSemaphore( vk::SemaphoreCreateInfo() );
|
2019-07-25 11:52:09 +00:00
|
|
|
}
|
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::Queue graphicsQueue = device.getQueue( graphicsAndPresentQueueFamilyIndex.first, 0 );
|
|
|
|
vk::Queue presentQueue = device.getQueue( graphicsAndPresentQueueFamilyIndex.second, 0 );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
// create a descriptor pool with a number of available descriptors
|
2020-04-12 19:49:12 +00:00
|
|
|
std::vector<vk::DescriptorPoolSize> poolSizes = {
|
2019-07-25 11:52:09 +00:00
|
|
|
{ vk::DescriptorType::eCombinedImageSampler, 1000 },
|
2020-04-12 19:49:12 +00:00
|
|
|
{ vk::DescriptorType::eUniformBuffer, 1000 },
|
2019-07-25 11:52:09 +00:00
|
|
|
{ vk::DescriptorType::eStorageBuffer, 1000 },
|
|
|
|
};
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::DescriptorPool descriptorPool = vk::su::createDescriptorPool( device, poolSizes );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
// setup swap chain, render pass, depth buffer and the frame buffers
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::su::SwapChainData swapChainData( physicalDevice,
|
|
|
|
device,
|
|
|
|
surface,
|
|
|
|
windowExtent,
|
|
|
|
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eStorage,
|
2021-02-17 09:49:59 +00:00
|
|
|
nullptr,
|
2020-04-12 19:49:12 +00:00
|
|
|
graphicsAndPresentQueueFamilyIndex.first,
|
|
|
|
graphicsAndPresentQueueFamilyIndex.second );
|
|
|
|
vk::SurfaceFormatKHR surfaceFormat = vk::su::pickSurfaceFormat( physicalDevice.getSurfaceFormatsKHR( surface ) );
|
|
|
|
vk::Format depthFormat = vk::su::pickDepthFormat( physicalDevice );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
// setup a render pass
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::RenderPass renderPass = vk::su::createRenderPass( device, surfaceFormat.format, depthFormat );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::su::DepthBufferData depthBufferData( physicalDevice, device, depthFormat, windowExtent );
|
2022-02-28 09:11:04 +00:00
|
|
|
std::vector<vk::Framebuffer> framebuffers =
|
|
|
|
vk::su::createFramebuffers( device, renderPass, swapChainData.imageViews, depthBufferData.imageView, windowExtent );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
2020-09-30 10:00:32 +00:00
|
|
|
bool samplerAnisotropy = !!supportedFeatures.get<vk::PhysicalDeviceFeatures2>().features.samplerAnisotropy;
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
// create some simple checkerboard textures, randomly sized and colored
|
2020-04-12 19:49:12 +00:00
|
|
|
const size_t textureCount = 10;
|
2019-07-25 11:52:09 +00:00
|
|
|
std::vector<vk::su::TextureData> textures;
|
2020-04-12 19:49:12 +00:00
|
|
|
textures.reserve( textureCount );
|
|
|
|
for ( size_t i = 0; i < textureCount; i++ )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
2021-02-17 09:49:59 +00:00
|
|
|
textures.emplace_back( physicalDevice,
|
|
|
|
device,
|
|
|
|
vk::Extent2D( random<uint32_t>( 2, 8 ) * 16, random<uint32_t>( 2, 8 ) * 16 ),
|
|
|
|
vk::ImageUsageFlagBits::eTransferDst | vk::ImageUsageFlagBits::eSampled,
|
|
|
|
vk::FormatFeatureFlags(),
|
|
|
|
samplerAnisotropy,
|
|
|
|
true );
|
2019-07-25 11:52:09 +00:00
|
|
|
}
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::su::oneTimeSubmit( device,
|
|
|
|
perFrameData[0].commandPool,
|
|
|
|
graphicsQueue,
|
|
|
|
[&]( vk::CommandBuffer const & commandBuffer )
|
|
|
|
{
|
|
|
|
for ( auto & t : textures )
|
|
|
|
{
|
|
|
|
t.setImage( device,
|
|
|
|
commandBuffer,
|
|
|
|
vk::su::CheckerboardImageGenerator( { random<uint8_t>(), random<uint8_t>(), random<uint8_t>() },
|
|
|
|
{ random<uint8_t>(), random<uint8_t>(), random<uint8_t>() } ) );
|
|
|
|
}
|
|
|
|
} );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
// create some materials with a random diffuse color, referencing one of the above textures
|
|
|
|
const size_t materialCount = 10;
|
2020-04-12 19:49:12 +00:00
|
|
|
assert( materialCount == textureCount );
|
|
|
|
std::vector<Material> materials( materialCount );
|
|
|
|
for ( size_t i = 0; i < materialCount; i++ )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
materials[i].diffuse = randomVec3( 0.0f, 1.0f );
|
|
|
|
materials[i].textureID = vk::su::checked_cast<uint32_t>( i );
|
2019-07-25 11:52:09 +00:00
|
|
|
}
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::su::BufferData materialBufferData( physicalDevice, device, materialCount * MaterialStride, vk::BufferUsageFlagBits::eStorageBuffer );
|
2020-04-12 19:49:12 +00:00
|
|
|
materialBufferData.upload( device, materials, MaterialStride );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
// create a a 3D-array of cubes, randomly jittered, using a random material
|
|
|
|
std::vector<Vertex> vertices;
|
2020-04-12 19:49:12 +00:00
|
|
|
vertices.reserve( xMax * yMax * zMax * cubeData.size() );
|
|
|
|
for ( size_t x = 0; x < xMax; x++ )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
for ( size_t y = 0; y < yMax; y++ )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
for ( size_t z = 0; z < zMax; z++ )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
int m = random<int>( 0, materialCount - 1 );
|
|
|
|
glm::vec3 jitter = randomVec3( 0.0f, 0.6f );
|
|
|
|
for ( auto const & v : cubeData )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
vertices.push_back( v );
|
2022-02-28 09:11:04 +00:00
|
|
|
vertices.back().pos += 3.0f * glm::vec3( static_cast<float>( x ), static_cast<float>( y ), static_cast<float>( z ) ) + jitter;
|
2020-04-12 19:49:12 +00:00
|
|
|
vertices.back().matID = static_cast<int>( m );
|
2019-07-25 11:52:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create an 1-1 index buffer
|
2020-04-12 19:49:12 +00:00
|
|
|
std::vector<unsigned int> indices( vertices.size() );
|
|
|
|
std::iota( indices.begin(), indices.end(), 0 );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
// there's just one vertex- and one index-buffer, but with more complex scene loaders there might be more!
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::su::BufferData vertexBufferData( physicalDevice,
|
|
|
|
device,
|
|
|
|
vertices.size() * VertexStride,
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::BufferUsageFlagBits::eTransferDst | vk::BufferUsageFlagBits::eVertexBuffer |
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::BufferUsageFlagBits::eStorageBuffer,
|
|
|
|
vk::MemoryPropertyFlagBits::eDeviceLocal );
|
2022-02-28 09:11:04 +00:00
|
|
|
vertexBufferData.upload( physicalDevice, device, perFrameData[0].commandPool, graphicsQueue, vertices, VertexStride );
|
2020-04-12 19:49:12 +00:00
|
|
|
|
|
|
|
vk::su::BufferData indexBufferData( physicalDevice,
|
|
|
|
device,
|
|
|
|
indices.size() * sizeof( uint32_t ),
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::BufferUsageFlagBits::eTransferDst | vk::BufferUsageFlagBits::eIndexBuffer | vk::BufferUsageFlagBits::eStorageBuffer,
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::MemoryPropertyFlagBits::eDeviceLocal );
|
2022-02-28 09:11:04 +00:00
|
|
|
indexBufferData.upload( physicalDevice, device, perFrameData[0].commandPool, graphicsQueue, indices, sizeof( uint32_t ) );
|
|
|
|
|
|
|
|
glm::mat4x4 transform( glm::mat4x4( 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f ) );
|
|
|
|
|
|
|
|
vk::DescriptorSetLayout descriptorSetLayout = vk::su::createDescriptorSetLayout(
|
|
|
|
device,
|
|
|
|
{ { vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex },
|
|
|
|
{ vk::DescriptorType::eStorageBuffer, 1, vk::ShaderStageFlagBits::eVertex | vk::ShaderStageFlagBits::eFragment },
|
|
|
|
{ vk::DescriptorType::eCombinedImageSampler, static_cast<uint32_t>( textures.size() ), vk::ShaderStageFlagBits::eFragment } } );
|
|
|
|
vk::PipelineLayout pipelineLayout = device.createPipelineLayout( vk::PipelineLayoutCreateInfo( {}, descriptorSetLayout ) );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
glslang::InitializeProcess();
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::ShaderModule vertexShaderModule = vk::su::createShaderModule( device, vk::ShaderStageFlagBits::eVertex, vertexShaderText );
|
|
|
|
vk::ShaderModule fragmentShaderModule = vk::su::createShaderModule( device, vk::ShaderStageFlagBits::eFragment, fragmentShaderText );
|
2019-07-25 11:52:09 +00:00
|
|
|
glslang::FinalizeProcess();
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::Pipeline graphicsPipeline =
|
|
|
|
vk::su::createGraphicsPipeline( device,
|
|
|
|
{},
|
|
|
|
std::make_pair( vertexShaderModule, nullptr ),
|
|
|
|
std::make_pair( fragmentShaderModule, nullptr ),
|
|
|
|
VertexStride,
|
|
|
|
{ { vk::Format::eR32G32B32Sfloat, vk::su::checked_cast<uint32_t>( offsetof( Vertex, pos ) ) },
|
|
|
|
{ vk::Format::eR32G32B32Sfloat, vk::su::checked_cast<uint32_t>( offsetof( Vertex, nrm ) ) },
|
|
|
|
{ vk::Format::eR32G32Sfloat, vk::su::checked_cast<uint32_t>( offsetof( Vertex, texCoord ) ) },
|
|
|
|
{ vk::Format::eR32Sint, vk::su::checked_cast<uint32_t>( offsetof( Vertex, matID ) ) } },
|
|
|
|
vk::FrontFace::eCounterClockwise,
|
|
|
|
true,
|
|
|
|
pipelineLayout,
|
|
|
|
renderPass );
|
|
|
|
|
|
|
|
vk::su::BufferData uniformBufferData( physicalDevice, device, sizeof( UniformBufferObject ), vk::BufferUsageFlagBits::eUniformBuffer );
|
2020-04-12 19:49:12 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::DescriptorSetAllocateInfo descriptorSetAllocateInfo( descriptorPool, descriptorSetLayout );
|
|
|
|
vk::DescriptorSet descriptorSet = device.allocateDescriptorSets( descriptorSetAllocateInfo ).front();
|
2022-05-02 07:43:45 +00:00
|
|
|
vk::su::updateDescriptorSets( device,
|
|
|
|
descriptorSet,
|
|
|
|
{ { vk::DescriptorType::eUniformBuffer, uniformBufferData.buffer, VK_WHOLE_SIZE, {} },
|
|
|
|
{ vk::DescriptorType::eStorageBuffer, materialBufferData.buffer, VK_WHOLE_SIZE, {} } },
|
|
|
|
textures );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
// RayTracing specific stuff
|
|
|
|
|
|
|
|
// create acceleration structures: one top-level, and just one bottom-level
|
|
|
|
AccelerationStructureData topLevelAS, bottomLevelAS;
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::su::oneTimeSubmit(
|
2022-02-28 09:11:04 +00:00
|
|
|
device,
|
|
|
|
perFrameData[0].commandPool,
|
|
|
|
graphicsQueue,
|
|
|
|
[&]( vk::CommandBuffer const & commandBuffer )
|
|
|
|
{
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::GeometryDataNV geometryDataNV( vk::GeometryTrianglesNV( vertexBufferData.buffer,
|
2020-04-12 19:49:12 +00:00
|
|
|
0,
|
|
|
|
vk::su::checked_cast<uint32_t>( vertices.size() ),
|
|
|
|
VertexStride,
|
|
|
|
vk::Format::eR32G32B32Sfloat,
|
2021-02-17 09:49:59 +00:00
|
|
|
indexBufferData.buffer,
|
2020-04-12 19:49:12 +00:00
|
|
|
0,
|
|
|
|
vk::su::checked_cast<uint32_t>( indices.size() ),
|
|
|
|
vk::IndexType::eUint32 ),
|
|
|
|
{} );
|
|
|
|
bottomLevelAS =
|
2022-02-28 09:11:04 +00:00
|
|
|
createAccelerationStructureData( physicalDevice, device, commandBuffer, {}, { vk::GeometryNV( vk::GeometryTypeNV::eTriangles, geometryDataNV ) } );
|
2020-04-12 19:49:12 +00:00
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
topLevelAS = createAccelerationStructureData(
|
|
|
|
physicalDevice, device, commandBuffer, { std::make_pair( bottomLevelAS.accelerationStructure, transform ) }, std::vector<vk::GeometryNV>() );
|
2020-04-12 19:49:12 +00:00
|
|
|
} );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
// create raytracing descriptor set
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::su::oneTimeSubmit(
|
2022-02-28 09:11:04 +00:00
|
|
|
device,
|
|
|
|
perFrameData[0].commandPool,
|
|
|
|
graphicsQueue,
|
|
|
|
[&]( vk::CommandBuffer const & commandBuffer )
|
|
|
|
{
|
|
|
|
vk::BufferMemoryBarrier bufferMemoryBarrier(
|
|
|
|
{}, vk::AccessFlagBits::eShaderRead, VK_QUEUE_FAMILY_IGNORED, VK_QUEUE_FAMILY_IGNORED, vertexBufferData.buffer, 0, VK_WHOLE_SIZE );
|
|
|
|
commandBuffer.pipelineBarrier(
|
|
|
|
vk::PipelineStageFlagBits::eAllCommands, vk::PipelineStageFlagBits::eAllCommands, {}, nullptr, bufferMemoryBarrier, nullptr );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
bufferMemoryBarrier.buffer = indexBufferData.buffer;
|
2022-02-28 09:11:04 +00:00
|
|
|
commandBuffer.pipelineBarrier(
|
|
|
|
vk::PipelineStageFlagBits::eAllCommands, vk::PipelineStageFlagBits::eAllCommands, {}, nullptr, bufferMemoryBarrier, nullptr );
|
2020-04-12 19:49:12 +00:00
|
|
|
} );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
std::vector<vk::DescriptorSetLayoutBinding> bindings;
|
2022-02-28 09:11:04 +00:00
|
|
|
bindings.emplace_back( 0, vk::DescriptorType::eAccelerationStructureNV, 1, vk::ShaderStageFlagBits::eRaygenNV | vk::ShaderStageFlagBits::eClosestHitNV );
|
|
|
|
bindings.emplace_back( 1, vk::DescriptorType::eStorageImage, 1, vk::ShaderStageFlagBits::eRaygenNV ); // raytracing output
|
|
|
|
bindings.emplace_back( 2, vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eRaygenNV ); // camera information
|
|
|
|
bindings.emplace_back( 3, vk::DescriptorType::eStorageBuffer, 1, vk::ShaderStageFlagBits::eClosestHitNV ); // vertex buffer
|
|
|
|
bindings.emplace_back( 4, vk::DescriptorType::eStorageBuffer, 1, vk::ShaderStageFlagBits::eClosestHitNV ); // index buffer
|
|
|
|
bindings.emplace_back( 5, vk::DescriptorType::eStorageBuffer, 1, vk::ShaderStageFlagBits::eClosestHitNV ); // material buffer
|
2021-02-17 09:49:59 +00:00
|
|
|
bindings.emplace_back( 6,
|
|
|
|
vk::DescriptorType::eCombinedImageSampler,
|
|
|
|
vk::su::checked_cast<uint32_t>( textures.size() ),
|
|
|
|
vk::ShaderStageFlagBits::eClosestHitNV ); // textures
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
std::vector<vk::DescriptorPoolSize> descriptorPoolSizes;
|
2020-04-12 19:49:12 +00:00
|
|
|
descriptorPoolSizes.reserve( bindings.size() );
|
|
|
|
for ( const auto & b : bindings )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
2022-02-28 09:11:04 +00:00
|
|
|
descriptorPoolSizes.emplace_back( b.descriptorType, vk::su::checked_cast<uint32_t>( swapChainData.images.size() ) * b.descriptorCount );
|
2019-07-25 11:52:09 +00:00
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
vk::DescriptorPoolCreateInfo descriptorPoolCreateInfo(
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet, vk::su::checked_cast<uint32_t>( swapChainData.images.size() ), descriptorPoolSizes );
|
|
|
|
vk::DescriptorPool rayTracingDescriptorPool = device.createDescriptorPool( descriptorPoolCreateInfo );
|
|
|
|
vk::DescriptorSetLayout rayTracingDescriptorSetLayout = device.createDescriptorSetLayout( vk::DescriptorSetLayoutCreateInfo( {}, bindings ) );
|
2019-12-19 12:59:48 +00:00
|
|
|
std::vector<vk::DescriptorSetLayout> layouts;
|
2020-04-12 19:49:12 +00:00
|
|
|
for ( size_t i = 0; i < swapChainData.images.size(); i++ )
|
2019-12-19 12:59:48 +00:00
|
|
|
{
|
2021-02-17 09:49:59 +00:00
|
|
|
layouts.push_back( rayTracingDescriptorSetLayout );
|
2019-12-19 12:59:48 +00:00
|
|
|
}
|
2022-02-28 09:11:04 +00:00
|
|
|
descriptorSetAllocateInfo = vk::DescriptorSetAllocateInfo( rayTracingDescriptorPool, layouts );
|
|
|
|
std::vector<vk::DescriptorSet> rayTracingDescriptorSets = device.allocateDescriptorSets( descriptorSetAllocateInfo );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
// Bind ray tracing specific descriptor sets into pNext of a vk::WriteDescriptorSet
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::WriteDescriptorSetAccelerationStructureNV writeDescriptorSetAcceleration( 1, &topLevelAS.accelerationStructure );
|
2020-04-12 19:49:12 +00:00
|
|
|
std::vector<vk::WriteDescriptorSet> accelerationDescriptionSets;
|
|
|
|
for ( size_t i = 0; i < rayTracingDescriptorSets.size(); i++ )
|
2019-12-19 12:59:48 +00:00
|
|
|
{
|
2022-03-23 16:08:19 +00:00
|
|
|
accelerationDescriptionSets.emplace_back(
|
|
|
|
rayTracingDescriptorSets[i], 0, 0, 1, bindings[0].descriptorType, nullptr, nullptr, nullptr, &writeDescriptorSetAcceleration );
|
2019-12-19 12:59:48 +00:00
|
|
|
}
|
2021-02-17 09:49:59 +00:00
|
|
|
device.updateDescriptorSets( accelerationDescriptionSets, nullptr );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
// Bind all the other buffers and images, starting with dstBinding == 2 (dstBinding == 1 is used by the backBuffer
|
|
|
|
// view)
|
|
|
|
for ( size_t i = 0; i < rayTracingDescriptorSets.size(); i++ )
|
2019-12-19 12:59:48 +00:00
|
|
|
{
|
2020-07-08 08:58:37 +00:00
|
|
|
vk::su::updateDescriptorSets( device,
|
|
|
|
rayTracingDescriptorSets[i],
|
2022-05-02 07:43:45 +00:00
|
|
|
{ { bindings[2].descriptorType, uniformBufferData.buffer, VK_WHOLE_SIZE, {} },
|
|
|
|
{ bindings[3].descriptorType, vertexBufferData.buffer, VK_WHOLE_SIZE, {} },
|
|
|
|
{ bindings[4].descriptorType, indexBufferData.buffer, VK_WHOLE_SIZE, {} },
|
|
|
|
{ bindings[5].descriptorType, materialBufferData.buffer, VK_WHOLE_SIZE, {} } },
|
2020-07-08 08:58:37 +00:00
|
|
|
textures,
|
|
|
|
2 );
|
2019-12-19 12:59:48 +00:00
|
|
|
}
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
// create the ray-tracing shader modules
|
|
|
|
glslang::InitializeProcess();
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::ShaderModule raygenShaderModule = vk::su::createShaderModule( device, vk::ShaderStageFlagBits::eRaygenNV, raygenShaderText );
|
|
|
|
vk::ShaderModule missShaderModule = vk::su::createShaderModule( device, vk::ShaderStageFlagBits::eMissNV, missShaderText );
|
|
|
|
vk::ShaderModule shadowMissShaderModule = vk::su::createShaderModule( device, vk::ShaderStageFlagBits::eMissNV, shadowMissShaderText );
|
|
|
|
vk::ShaderModule closestHitShaderModule = vk::su::createShaderModule( device, vk::ShaderStageFlagBits::eClosestHitNV, closestHitShaderText );
|
2019-07-25 11:52:09 +00:00
|
|
|
glslang::FinalizeProcess();
|
|
|
|
|
|
|
|
// create the ray tracing pipeline
|
2020-04-12 19:49:12 +00:00
|
|
|
std::vector<vk::PipelineShaderStageCreateInfo> shaderStages;
|
2019-07-25 11:52:09 +00:00
|
|
|
std::vector<vk::RayTracingShaderGroupCreateInfoNV> shaderGroups;
|
|
|
|
|
|
|
|
// We use only one ray generation, that will implement the camera model
|
2022-02-28 09:11:04 +00:00
|
|
|
shaderStages.emplace_back( vk::PipelineShaderStageCreateFlags(), vk::ShaderStageFlagBits::eRaygenNV, raygenShaderModule, "main" );
|
|
|
|
shaderGroups.emplace_back( vk::RayTracingShaderGroupTypeNV::eGeneral, 0, VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
// The first miss shader is used to look-up the environment in case the rays from the camera miss the geometry
|
2022-02-28 09:11:04 +00:00
|
|
|
shaderStages.emplace_back( vk::PipelineShaderStageCreateFlags(), vk::ShaderStageFlagBits::eMissNV, missShaderModule, "main" );
|
|
|
|
shaderGroups.emplace_back( vk::RayTracingShaderGroupTypeNV::eGeneral, 1, VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV );
|
2020-04-12 19:49:12 +00:00
|
|
|
|
|
|
|
// The second miss shader is invoked when a shadow ray misses the geometry. It simply indicates that no occlusion
|
|
|
|
// has been found
|
2022-02-28 09:11:04 +00:00
|
|
|
shaderStages.emplace_back( vk::PipelineShaderStageCreateFlags(), vk::ShaderStageFlagBits::eMissNV, shadowMissShaderModule, "main" );
|
|
|
|
shaderGroups.emplace_back( vk::RayTracingShaderGroupTypeNV::eGeneral, 2, VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV );
|
2020-04-12 19:49:12 +00:00
|
|
|
|
|
|
|
// The first hit group defines the shaders invoked when a ray shot from the camera hit the geometry. In this case we
|
|
|
|
// only specify the closest hit shader, and rely on the build-in triangle intersection and pass-through any-hit
|
|
|
|
// shader. However, explicit intersection and any hit shaders could be added as well.
|
2022-02-28 09:11:04 +00:00
|
|
|
shaderStages.emplace_back( vk::PipelineShaderStageCreateFlags(), vk::ShaderStageFlagBits::eClosestHitNV, closestHitShaderModule, "main" );
|
|
|
|
shaderGroups.emplace_back( vk::RayTracingShaderGroupTypeNV::eTrianglesHitGroup, VK_SHADER_UNUSED_NV, 3, VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV );
|
2020-04-12 19:49:12 +00:00
|
|
|
|
|
|
|
// The second hit group defines the shaders invoked when a shadow ray hits the geometry. For simple shadows we do
|
|
|
|
// not need any shader in that group: we will rely on initializing the payload and update it only in the miss shader
|
2022-02-28 09:11:04 +00:00
|
|
|
shaderGroups.emplace_back(
|
|
|
|
vk::RayTracingShaderGroupTypeNV::eTrianglesHitGroup, VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV, VK_SHADER_UNUSED_NV );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
// Create the layout of the pipeline following the provided descriptor set layout
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::PipelineLayout rayTracingPipelineLayout = device.createPipelineLayout( vk::PipelineLayoutCreateInfo( {}, rayTracingDescriptorSetLayout ) );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
// Assemble the shader stages and recursion depth info into the raytracing pipeline
|
|
|
|
// The ray tracing process can shoot rays from the camera, and a shadow ray can be shot from the
|
|
|
|
// hit points of the camera rays, hence a recursion level of 2. This number should be kept as low
|
|
|
|
// as possible for performance reasons. Even recursive ray tracing should be flattened into a loop
|
|
|
|
// in the ray generation to avoid deep recursion.
|
2020-04-12 19:49:12 +00:00
|
|
|
uint32_t maxRecursionDepth = 2;
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::RayTracingPipelineCreateInfoNV rayTracingPipelineCreateInfo( {}, shaderStages, shaderGroups, maxRecursionDepth, rayTracingPipelineLayout );
|
|
|
|
vk::Pipeline rayTracingPipeline;
|
|
|
|
vk::ResultValue<vk::Pipeline> rvPipeline = device.createRayTracingPipelineNV( nullptr, rayTracingPipelineCreateInfo );
|
2020-07-20 11:48:03 +00:00
|
|
|
switch ( rvPipeline.result )
|
|
|
|
{
|
2021-02-17 09:49:59 +00:00
|
|
|
case vk::Result::eSuccess: rayTracingPipeline = rvPipeline.value; break;
|
2020-07-20 11:48:03 +00:00
|
|
|
case vk::Result::ePipelineCompileRequiredEXT:
|
|
|
|
// something meaningfull here
|
|
|
|
break;
|
|
|
|
default: assert( false ); // should never happen
|
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
|
2020-08-12 09:38:42 +00:00
|
|
|
vk::StructureChain<vk::PhysicalDeviceProperties2, vk::PhysicalDeviceRayTracingPropertiesNV> propertiesChain =
|
|
|
|
physicalDevice.getProperties2<vk::PhysicalDeviceProperties2, vk::PhysicalDeviceRayTracingPropertiesNV>();
|
2022-02-28 09:11:04 +00:00
|
|
|
uint32_t shaderGroupBaseAlignment = propertiesChain.get<vk::PhysicalDeviceRayTracingPropertiesNV>().shaderGroupBaseAlignment;
|
|
|
|
uint32_t shaderGroupHandleSize = propertiesChain.get<vk::PhysicalDeviceRayTracingPropertiesNV>().shaderGroupHandleSize;
|
2020-08-12 09:38:42 +00:00
|
|
|
|
2020-09-30 10:00:32 +00:00
|
|
|
uint32_t raygenShaderBindingOffset = 0; // starting with raygen
|
|
|
|
uint32_t raygenShaderTableSize = shaderGroupHandleSize; // one raygen shader
|
2022-02-28 09:11:04 +00:00
|
|
|
uint32_t missShaderBindingOffset = raygenShaderBindingOffset + roundUp( raygenShaderTableSize, shaderGroupBaseAlignment );
|
|
|
|
uint32_t missShaderBindingStride = shaderGroupHandleSize;
|
|
|
|
uint32_t missShaderTableSize = 2 * missShaderBindingStride; // two raygen shaders
|
|
|
|
uint32_t hitShaderBindingOffset = missShaderBindingOffset + roundUp( missShaderTableSize, shaderGroupBaseAlignment );
|
|
|
|
uint32_t hitShaderBindingStride = shaderGroupHandleSize;
|
|
|
|
uint32_t hitShaderTableSize = 2 * hitShaderBindingStride; // two hit shaders
|
2020-08-12 09:38:42 +00:00
|
|
|
|
2020-09-30 10:00:32 +00:00
|
|
|
uint32_t shaderBindingTableSize = hitShaderBindingOffset + hitShaderTableSize;
|
2020-04-12 19:49:12 +00:00
|
|
|
std::vector<uint8_t> shaderHandleStorage( shaderBindingTableSize );
|
2022-02-28 09:11:04 +00:00
|
|
|
(void)device.getRayTracingShaderGroupHandlesNV( rayTracingPipeline, 0, 1, raygenShaderTableSize, &shaderHandleStorage[raygenShaderBindingOffset] );
|
|
|
|
(void)device.getRayTracingShaderGroupHandlesNV( rayTracingPipeline, 1, 2, missShaderTableSize, &shaderHandleStorage[missShaderBindingOffset] );
|
|
|
|
(void)device.getRayTracingShaderGroupHandlesNV( rayTracingPipeline, 3, 2, hitShaderTableSize, &shaderHandleStorage[hitShaderBindingOffset] );
|
|
|
|
|
|
|
|
vk::su::BufferData shaderBindingTableBufferData(
|
|
|
|
physicalDevice, device, shaderBindingTableSize, vk::BufferUsageFlagBits::eTransferDst, vk::MemoryPropertyFlagBits::eHostVisible );
|
2020-04-12 19:49:12 +00:00
|
|
|
shaderBindingTableBufferData.upload( device, shaderHandleStorage );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
std::array<vk::ClearValue, 2> clearValues;
|
2020-05-18 12:02:37 +00:00
|
|
|
clearValues[0].color = vk::ClearColorValue( std::array<float, 4>( { { 0.2f, 0.2f, 0.2f, 0.2f } } ) );
|
2020-04-12 19:49:12 +00:00
|
|
|
clearValues[1].depthStencil = vk::ClearDepthStencilValue( 1.0f, 0 );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
// Main loop
|
2020-04-12 19:49:12 +00:00
|
|
|
uint32_t frameIndex = 0;
|
2019-07-25 11:52:09 +00:00
|
|
|
UniformBufferObject uniformBufferObject;
|
2020-04-12 19:49:12 +00:00
|
|
|
uniformBufferObject.model = glm::mat4( 1 );
|
|
|
|
uniformBufferObject.modelIT = glm::inverseTranspose( uniformBufferObject.model );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
double accumulatedTime{ 0.0 };
|
|
|
|
size_t frameCount{ 0 };
|
|
|
|
while ( !glfwWindowShouldClose( window ) )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
|
|
|
double startTime = glfwGetTime();
|
|
|
|
glfwPollEvents();
|
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::CommandBuffer const & commandBuffer = perFrameData[frameIndex].commandBuffer;
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
int w, h;
|
2020-04-12 19:49:12 +00:00
|
|
|
glfwGetWindowSize( window, &w, &h );
|
|
|
|
if ( ( w != static_cast<int>( windowExtent.width ) ) || ( h != static_cast<int>( windowExtent.height ) ) )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
windowExtent.width = w;
|
2019-07-25 11:52:09 +00:00
|
|
|
windowExtent.height = h;
|
2021-02-17 09:49:59 +00:00
|
|
|
device.waitIdle();
|
2022-02-28 09:11:04 +00:00
|
|
|
swapChainData = vk::su::SwapChainData( physicalDevice,
|
|
|
|
device,
|
|
|
|
surface,
|
|
|
|
windowExtent,
|
|
|
|
vk::ImageUsageFlagBits::eColorAttachment | vk::ImageUsageFlagBits::eStorage,
|
|
|
|
swapChainData.swapChain,
|
|
|
|
graphicsAndPresentQueueFamilyIndex.first,
|
|
|
|
graphicsAndPresentQueueFamilyIndex.second );
|
|
|
|
depthBufferData = vk::su::DepthBufferData( physicalDevice, device, vk::su::pickDepthFormat( physicalDevice ), windowExtent );
|
|
|
|
|
|
|
|
vk::su::oneTimeSubmit(
|
|
|
|
commandBuffer,
|
|
|
|
graphicsQueue,
|
|
|
|
[&]( vk::CommandBuffer const & commandBuffer )
|
|
|
|
{
|
|
|
|
vk::su::setImageLayout(
|
|
|
|
commandBuffer, depthBufferData.image, depthFormat, vk::ImageLayout::eUndefined, vk::ImageLayout::eDepthStencilAttachmentOptimal );
|
|
|
|
} );
|
|
|
|
|
|
|
|
framebuffers = vk::su::createFramebuffers( device, renderPass, swapChainData.imageViews, depthBufferData.imageView, windowExtent );
|
2019-07-25 11:52:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// update the uniformBufferObject
|
2020-04-12 19:49:12 +00:00
|
|
|
assert( 0 < windowExtent.height );
|
2019-07-25 11:52:09 +00:00
|
|
|
uniformBufferObject.view = appInfo.cameraManipulator.getMatrix();
|
2022-02-28 09:11:04 +00:00
|
|
|
uniformBufferObject.proj = glm::perspective( glm::radians( 65.0f ), windowExtent.width / static_cast<float>( windowExtent.height ), 0.1f, 1000.0f );
|
2019-07-25 11:52:09 +00:00
|
|
|
uniformBufferObject.proj[1][1] *= -1; // Inverting Y for Vulkan
|
2020-04-12 19:49:12 +00:00
|
|
|
uniformBufferObject.viewInverse = glm::inverse( uniformBufferObject.view );
|
|
|
|
uniformBufferObject.projInverse = glm::inverse( uniformBufferObject.proj );
|
|
|
|
uniformBufferData.upload( device, uniformBufferObject );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
// frame begin
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::ResultValue<uint32_t> rv =
|
|
|
|
device.acquireNextImageKHR( swapChainData.swapChain, UINT64_MAX, perFrameData[frameIndex].presentCompleteSemaphore, nullptr );
|
2020-04-12 19:49:12 +00:00
|
|
|
assert( rv.result == vk::Result::eSuccess );
|
2019-07-25 11:52:09 +00:00
|
|
|
uint32_t backBufferIndex = rv.value;
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
while ( vk::Result::eTimeout == device.waitForFences( perFrameData[frameIndex].fence, VK_TRUE, vk::su::FenceTimeout ) )
|
2019-10-28 14:36:21 +00:00
|
|
|
;
|
2021-02-17 09:49:59 +00:00
|
|
|
device.resetFences( perFrameData[frameIndex].fence );
|
2019-10-28 14:36:21 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
commandBuffer.begin( vk::CommandBufferBeginInfo( vk::CommandBufferUsageFlagBits::eOneTimeSubmit ) );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( appInfo.useRasterRender )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
2021-02-17 09:49:59 +00:00
|
|
|
commandBuffer.beginRenderPass(
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::RenderPassBeginInfo( renderPass, framebuffers[backBufferIndex], vk::Rect2D( vk::Offset2D( 0, 0 ), windowExtent ), clearValues ),
|
2021-02-17 09:49:59 +00:00
|
|
|
vk::SubpassContents::eInline );
|
|
|
|
|
|
|
|
commandBuffer.bindPipeline( vk::PipelineBindPoint::eGraphics, graphicsPipeline );
|
|
|
|
commandBuffer.bindDescriptorSets( vk::PipelineBindPoint::eGraphics, pipelineLayout, 0, descriptorSet, nullptr );
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
commandBuffer.setViewport(
|
|
|
|
0, vk::Viewport( 0.0f, 0.0f, static_cast<float>( windowExtent.width ), static_cast<float>( windowExtent.height ), 0.0f, 1.0f ) );
|
2021-02-17 09:49:59 +00:00
|
|
|
commandBuffer.setScissor( 0, vk::Rect2D( vk::Offset2D( 0, 0 ), windowExtent ) );
|
|
|
|
|
|
|
|
commandBuffer.bindVertexBuffers( 0, vertexBufferData.buffer, { 0 } );
|
|
|
|
commandBuffer.bindIndexBuffer( indexBufferData.buffer, 0, vk::IndexType::eUint32 );
|
|
|
|
commandBuffer.drawIndexed( vk::su::checked_cast<uint32_t>( indices.size() ), 1, 0, 0, 0 );
|
|
|
|
|
|
|
|
commandBuffer.endRenderPass();
|
2019-07-25 11:52:09 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::DescriptorImageInfo imageInfo( nullptr, swapChainData.imageViews[backBufferIndex], vk::ImageLayout::eGeneral );
|
|
|
|
vk::WriteDescriptorSet writeDescriptorSet( rayTracingDescriptorSets[backBufferIndex], 1, 0, bindings[1].descriptorType, imageInfo );
|
2021-02-17 09:49:59 +00:00
|
|
|
device.updateDescriptorSets( writeDescriptorSet, nullptr );
|
2020-04-12 19:49:12 +00:00
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::su::setImageLayout(
|
|
|
|
commandBuffer, swapChainData.images[backBufferIndex], surfaceFormat.format, vk::ImageLayout::eUndefined, vk::ImageLayout::eGeneral );
|
2020-04-12 19:49:12 +00:00
|
|
|
|
2021-02-17 09:49:59 +00:00
|
|
|
commandBuffer.bindPipeline( vk::PipelineBindPoint::eRayTracingNV, rayTracingPipeline );
|
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
commandBuffer.bindDescriptorSets(
|
|
|
|
vk::PipelineBindPoint::eRayTracingNV, rayTracingPipelineLayout, 0, rayTracingDescriptorSets[backBufferIndex], nullptr );
|
2021-02-17 09:49:59 +00:00
|
|
|
|
|
|
|
commandBuffer.traceRaysNV( shaderBindingTableBufferData.buffer,
|
|
|
|
raygenShaderBindingOffset,
|
|
|
|
shaderBindingTableBufferData.buffer,
|
|
|
|
missShaderBindingOffset,
|
|
|
|
missShaderBindingStride,
|
|
|
|
shaderBindingTableBufferData.buffer,
|
|
|
|
hitShaderBindingOffset,
|
|
|
|
hitShaderBindingStride,
|
|
|
|
nullptr,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
windowExtent.width,
|
|
|
|
windowExtent.height,
|
|
|
|
1 );
|
2020-04-12 19:49:12 +00:00
|
|
|
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::su::setImageLayout(
|
|
|
|
commandBuffer, swapChainData.images[backBufferIndex], surfaceFormat.format, vk::ImageLayout::eGeneral, vk::ImageLayout::ePresentSrcKHR );
|
2019-12-19 12:59:48 +00:00
|
|
|
}
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
// frame end
|
2021-02-17 09:49:59 +00:00
|
|
|
commandBuffer.end();
|
2019-07-25 11:52:09 +00:00
|
|
|
const vk::PipelineStageFlags waitDstStageMask = vk::PipelineStageFlagBits::eColorAttachmentOutput;
|
2020-04-12 19:49:12 +00:00
|
|
|
graphicsQueue.submit( vk::SubmitInfo( 1,
|
2021-02-17 09:49:59 +00:00
|
|
|
&( perFrameData[frameIndex].presentCompleteSemaphore ),
|
2020-04-12 19:49:12 +00:00
|
|
|
&waitDstStageMask,
|
|
|
|
1,
|
2021-02-17 09:49:59 +00:00
|
|
|
&commandBuffer,
|
2020-04-12 19:49:12 +00:00
|
|
|
1,
|
2021-02-17 09:49:59 +00:00
|
|
|
&( perFrameData[frameIndex].renderCompleteSemaphore ) ),
|
|
|
|
perFrameData[frameIndex].fence );
|
2022-02-28 09:11:04 +00:00
|
|
|
vk::Result result =
|
|
|
|
presentQueue.presentKHR( vk::PresentInfoKHR( perFrameData[frameIndex].renderCompleteSemaphore, swapChainData.swapChain, backBufferIndex ) );
|
2020-07-15 06:55:47 +00:00
|
|
|
switch ( result )
|
|
|
|
{
|
|
|
|
case vk::Result::eSuccess: break;
|
2022-02-28 09:11:04 +00:00
|
|
|
case vk::Result::eSuboptimalKHR: std::cout << "vk::Queue::presentKHR returned vk::Result::eSuboptimalKHR !\n"; break;
|
2020-07-15 06:55:47 +00:00
|
|
|
default: assert( false ); // an unexpected result is returned !
|
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
frameIndex = ( frameIndex + 1 ) % IMGUI_VK_QUEUED_FRAMES;
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
double endTime = glfwGetTime();
|
|
|
|
accumulatedTime += endTime - startTime;
|
|
|
|
++frameCount;
|
2020-04-12 19:49:12 +00:00
|
|
|
if ( 1.0 < accumulatedTime )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
2020-04-12 19:49:12 +00:00
|
|
|
assert( 0 < frameCount );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
std::ostringstream oss;
|
2022-02-28 09:11:04 +00:00
|
|
|
oss << AppName << ": " << vertices.size() << " Vertices " << ( appInfo.useRasterRender ? "Rastering" : "RayTracing" ) << " ( "
|
|
|
|
<< frameCount / accumulatedTime << " fps)";
|
2020-04-12 19:49:12 +00:00
|
|
|
glfwSetWindowTitle( window, oss.str().c_str() );
|
2019-07-25 11:52:09 +00:00
|
|
|
|
|
|
|
accumulatedTime = 0.0;
|
2020-04-12 19:49:12 +00:00
|
|
|
frameCount = 0;
|
2019-07-25 11:52:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup
|
2021-02-17 09:49:59 +00:00
|
|
|
device.waitIdle();
|
|
|
|
|
|
|
|
shaderBindingTableBufferData.clear( device );
|
|
|
|
device.destroyPipeline( rayTracingPipeline );
|
|
|
|
device.destroyPipelineLayout( rayTracingPipelineLayout );
|
|
|
|
device.destroyShaderModule( closestHitShaderModule );
|
|
|
|
device.destroyShaderModule( shadowMissShaderModule );
|
|
|
|
device.destroyShaderModule( missShaderModule );
|
|
|
|
device.destroyShaderModule( raygenShaderModule );
|
|
|
|
device.freeDescriptorSets( rayTracingDescriptorPool, rayTracingDescriptorSets );
|
|
|
|
device.destroyDescriptorSetLayout( rayTracingDescriptorSetLayout );
|
|
|
|
device.destroyDescriptorPool( rayTracingDescriptorPool );
|
|
|
|
topLevelAS.clear( device );
|
|
|
|
bottomLevelAS.clear( device );
|
|
|
|
device.freeDescriptorSets( descriptorPool, descriptorSet );
|
|
|
|
uniformBufferData.clear( device );
|
|
|
|
device.destroyPipeline( graphicsPipeline );
|
|
|
|
device.destroyShaderModule( fragmentShaderModule );
|
|
|
|
device.destroyShaderModule( vertexShaderModule );
|
|
|
|
device.destroyPipelineLayout( pipelineLayout );
|
|
|
|
device.destroyDescriptorSetLayout( descriptorSetLayout );
|
|
|
|
indexBufferData.clear( device );
|
|
|
|
vertexBufferData.clear( device );
|
|
|
|
materialBufferData.clear( device );
|
|
|
|
for ( auto & texture : textures )
|
|
|
|
{
|
|
|
|
texture.clear( device );
|
|
|
|
}
|
|
|
|
for ( auto framebuffer : framebuffers )
|
|
|
|
{
|
|
|
|
device.destroyFramebuffer( framebuffer );
|
|
|
|
}
|
|
|
|
depthBufferData.clear( device );
|
|
|
|
device.destroyRenderPass( renderPass );
|
|
|
|
swapChainData.clear( device );
|
|
|
|
device.destroyDescriptorPool( descriptorPool );
|
|
|
|
for ( int i = 0; i < IMGUI_VK_QUEUED_FRAMES; i++ )
|
|
|
|
{
|
|
|
|
perFrameData[i].clear( device );
|
|
|
|
}
|
|
|
|
device.destroy();
|
|
|
|
instance.destroySurfaceKHR( surface );
|
2021-05-25 14:14:56 +00:00
|
|
|
#if !defined( NDEBUG )
|
2021-02-17 09:49:59 +00:00
|
|
|
instance.destroyDebugUtilsMessengerEXT( debugUtilsMessenger );
|
2021-05-25 14:14:56 +00:00
|
|
|
#endif
|
2021-02-17 09:49:59 +00:00
|
|
|
instance.destroy();
|
|
|
|
|
2020-04-12 19:49:12 +00:00
|
|
|
glfwDestroyWindow( window );
|
2019-07-25 11:52:09 +00:00
|
|
|
glfwTerminate();
|
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
catch ( vk::SystemError & err )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
|
|
|
std::cout << "vk::SystemError: " << err.what() << std::endl;
|
2020-04-12 19:49:12 +00:00
|
|
|
exit( -1 );
|
2019-07-25 11:52:09 +00:00
|
|
|
}
|
2020-07-08 08:58:37 +00:00
|
|
|
catch ( std::exception & err )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
2020-07-08 08:58:37 +00:00
|
|
|
std::cout << "std::exception: " << err.what() << std::endl;
|
2020-04-12 19:49:12 +00:00
|
|
|
exit( -1 );
|
2019-07-25 11:52:09 +00:00
|
|
|
}
|
2020-04-12 19:49:12 +00:00
|
|
|
catch ( ... )
|
2019-07-25 11:52:09 +00:00
|
|
|
{
|
|
|
|
std::cout << "unknown error\n";
|
2020-04-12 19:49:12 +00:00
|
|
|
exit( -1 );
|
2019-07-25 11:52:09 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|