back to a working state

This commit is contained in:
Dynamitos
2025-01-28 19:44:47 +01:00
parent e37fef6d26
commit 49d2c47b16
11 changed files with 570 additions and 319 deletions
+8
View File
@@ -0,0 +1,8 @@
import Common;
[shader("miss")]
void miss(inout RayPayload p)
{
p.light = float3(0, 0, 0);
p.hit = false;
}
+234 -111
View File
@@ -1,4 +1,5 @@
#include "GPURenderer.h" #include "GPURenderer.h"
#include "util/ModelLoader.h"
#include "vulkan/vulkan_enums.hpp" #include "vulkan/vulkan_enums.hpp"
#include "vulkan/vulkan_handles.hpp" #include "vulkan/vulkan_handles.hpp"
#include "vulkan/vulkan_raii.hpp" #include "vulkan/vulkan_raii.hpp"
@@ -9,16 +10,11 @@
#include "vk_mem_alloc.h" #include "vk_mem_alloc.h"
GPURenderer::GPURenderer() GPURenderer::GPURenderer()
: instance(nullptr), physicalDevice(nullptr), device(nullptr), queue(nullptr), cmdPool(nullptr), cmdBuffers(nullptr),
descriptorLayout(nullptr), descriptorSet(nullptr), descriptorPool(nullptr), pipelineLayout(nullptr), rayGen(nullptr),
closestHit(nullptr), miss(nullptr), pipeline(nullptr), radianceAccumulator(nullptr), radianceAllocation(nullptr), image(nullptr),
imageAllocation(nullptr)
{ {
createDevice(); createDevice();
createCommands(); createCommands();
createDescriptors(); createDescriptors();
createShaders(); createPipeline();
} }
GPURenderer::~GPURenderer() {} GPURenderer::~GPURenderer() {}
@@ -40,6 +36,12 @@ void GPURenderer::createDevice()
} }
} }
} }
auto properties = physicalDevice.getProperties2<vk::PhysicalDeviceProperties2, vk::PhysicalDeviceAccelerationStructurePropertiesKHR,
vk::PhysicalDeviceRayTracingPipelinePropertiesKHR>();
accelerationProperties = properties.get<vk::PhysicalDeviceAccelerationStructurePropertiesKHR>();
rayTracingProperties = properties.get<vk::PhysicalDeviceRayTracingPipelinePropertiesKHR>();
uint32_t computeQueueFamily = 0; uint32_t computeQueueFamily = 0;
auto queueProps = physicalDevice.getQueueFamilyProperties(); auto queueProps = physicalDevice.getQueueFamilyProperties();
for (uint32_t i = 0; i < queueProps.size(); ++i) for (uint32_t i = 0; i < queueProps.size(); ++i)
@@ -50,22 +52,31 @@ void GPURenderer::createDevice()
break; break;
} }
} }
float queuePriority = 0.0f; std::vector<float> queuePriority = {1.0f};
vk::DeviceQueueCreateInfo deviceQueueCreateInfo({}, computeQueueFamily, 1, &queuePriority); auto featureChain = physicalDevice.getFeatures2<vk::PhysicalDeviceFeatures2, vk::PhysicalDeviceRayTracingPipelineFeaturesKHR,
vk::DeviceCreateInfo deviceCreateInfo({}, deviceQueueCreateInfo); vk::PhysicalDeviceAccelerationStructureFeaturesKHR>();
auto features = featureChain.get<vk::PhysicalDeviceFeatures2>();
vk::DeviceQueueCreateInfo deviceQueueCreateInfo({}, computeQueueFamily, queuePriority);
const char* extensions[] = {vk::KHRAccelerationStructureExtensionName, vk::KHRRayTracingPipelineExtensionName, vk::KHRDeferredHostOperationsExtensionName};
vk::DeviceCreateInfo deviceCreateInfo({}, deviceQueueCreateInfo, {}, extensions, nullptr, &features);
device = Device(physicalDevice, deviceCreateInfo); device = Device(physicalDevice, deviceCreateInfo);
VmaVulkanFunctions vulkanFunctions = {}; queue = Queue(device, computeQueueFamily, 0);
vulkanFunctions.vkGetInstanceProcAddr = &vkGetInstanceProcAddr;
vulkanFunctions.vkGetDeviceProcAddr = &vkGetDeviceProcAddr;
VmaAllocatorCreateInfo allocatorCreateInfo = {}; VmaVulkanFunctions vulkanFunctions = {
allocatorCreateInfo.flags = VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT; .vkGetInstanceProcAddr = &vkGetInstanceProcAddr,
allocatorCreateInfo.vulkanApiVersion = VK_API_VERSION_1_2; .vkGetDeviceProcAddr = &vkGetDeviceProcAddr,
allocatorCreateInfo.physicalDevice = *physicalDevice; };
allocatorCreateInfo.device = *device;
allocatorCreateInfo.instance = *instance; VmaAllocatorCreateInfo allocatorCreateInfo = {
allocatorCreateInfo.pVulkanFunctions = &vulkanFunctions; .flags = VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT | VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT,
.physicalDevice = *physicalDevice,
.device = *device,
.pVulkanFunctions = &vulkanFunctions,
.instance = *instance,
.vulkanApiVersion = VK_API_VERSION_1_3,
};
vmaCreateAllocator(&allocatorCreateInfo, &allocator); vmaCreateAllocator(&allocatorCreateInfo, &allocator);
} }
@@ -126,28 +137,35 @@ vk::DescriptorPoolSize(vk::DescriptorType::eAccelerationStructureKHR, 1),
vk::DescriptorPoolSize(vk::DescriptorType::eStorageImage, 2), vk::DescriptorPoolSize(vk::DescriptorType::eStorageImage, 2),
vk::DescriptorPoolSize(vk::DescriptorType::eStorageBuffer, 8), vk::DescriptorPoolSize(vk::DescriptorType::eStorageBuffer, 8),
}; };
descriptorPool = DescriptorPool(device, vk::DescriptorPoolCreateInfo({}, 4, descriptorPoolSizes)); descriptorPool =
DescriptorPool(device, vk::DescriptorPoolCreateInfo({vk::DescriptorPoolCreateFlagBits::eFreeDescriptorSet}, 4, descriptorPoolSizes));
// create a PipelineLayout using that DescriptorSetLayout // create a PipelineLayout using that DescriptorSetLayout
vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo({}, *descriptorLayout); vk::PushConstantRange range =
vk::PushConstantRange(vk::ShaderStageFlagBits::eRaygenKHR | vk::ShaderStageFlagBits::eClosestHitKHR, 0, sizeof(SampleParams));
vk::PipelineLayoutCreateInfo pipelineLayoutCreateInfo({}, *descriptorLayout, range);
pipelineLayout = PipelineLayout(device, pipelineLayoutCreateInfo); pipelineLayout = PipelineLayout(device, pipelineLayoutCreateInfo);
} }
using namespace slang; using namespace slang;
void GPURenderer::createShaders() template <typename T> constexpr T align(T size, T alignment) { return (size + alignment - 1) & ~(alignment - 1); }
void GPURenderer::createPipeline()
{ {
Slang::ComPtr<IGlobalSession> globalSession; Slang::ComPtr<IGlobalSession> globalSession;
createGlobalSession(globalSession.writeRef()); createGlobalSession(globalSession.writeRef());
SessionDesc sessionDesc; TargetDesc targetDesc = {
TargetDesc targetDesc; .format = SLANG_SPIRV,
targetDesc.format = SLANG_SPIRV; .profile = globalSession->findProfile("glsl_450"),
targetDesc.profile = globalSession->findProfile("glsl_450"); };
sessionDesc.targets = &targetDesc; const char* searchPaths[] = {"../res/shaders/"};
sessionDesc.targetCount = 1; SessionDesc sessionDesc = {
const char* searchPaths[] = {"res/shaders/"}; .targets = &targetDesc,
sessionDesc.searchPaths = searchPaths; .targetCount = 1,
sessionDesc.searchPathCount = 1; .searchPaths = searchPaths,
.searchPathCount = 1,
};
Slang::ComPtr<ISession> session; Slang::ComPtr<ISession> session;
globalSession->createSession(sessionDesc, session.writeRef()); globalSession->createSession(sessionDesc, session.writeRef());
@@ -158,7 +176,7 @@ void GPURenderer::createShaders()
std::cout << (const char*)diagnostics->getBufferPointer() << std::endl; std::cout << (const char*)diagnostics->getBufferPointer() << std::endl;
} }
Slang::ComPtr<IEntryPoint> rayGenEntry; Slang::ComPtr<IEntryPoint> rayGenEntry;
raygenModule->findEntryPointByName("rayGen", rayGenEntry.writeRef()); raygenModule->findEntryPointByName("raygen", rayGenEntry.writeRef());
IModule* closestHitModule = session->loadModule("ClosestHit", diagnostics.writeRef()); IModule* closestHitModule = session->loadModule("ClosestHit", diagnostics.writeRef());
if (diagnostics) if (diagnostics)
@@ -168,9 +186,17 @@ void GPURenderer::createShaders()
Slang::ComPtr<IEntryPoint> closestHitEntry; Slang::ComPtr<IEntryPoint> closestHitEntry;
closestHitModule->findEntryPointByName("closestHit", closestHitEntry.writeRef()); closestHitModule->findEntryPointByName("closestHit", closestHitEntry.writeRef());
IComponentType* components[] = {raygenModule, rayGenEntry, closestHitModule, closestHitEntry}; IModule* missModule = session->loadModule("Miss", diagnostics.writeRef());
if (diagnostics)
{
std::cout << (const char*)diagnostics->getBufferPointer() << std::endl;
}
Slang::ComPtr<IEntryPoint> missEntry;
missModule->findEntryPointByName("miss", missEntry.writeRef());
IComponentType* components[] = {raygenModule, rayGenEntry, closestHitModule, closestHitEntry, missModule, missEntry};
Slang::ComPtr<IComponentType> program; Slang::ComPtr<IComponentType> program;
session->createCompositeComponentType(components, 4, program.writeRef()); session->createCompositeComponentType(components, 6, program.writeRef());
Slang::ComPtr<IComponentType> linkedProgram; Slang::ComPtr<IComponentType> linkedProgram;
program->link(linkedProgram.writeRef(), diagnostics.writeRef()); program->link(linkedProgram.writeRef(), diagnostics.writeRef());
@@ -181,46 +207,28 @@ void GPURenderer::createShaders()
Slang::ComPtr<IBlob> closestHitCode; Slang::ComPtr<IBlob> closestHitCode;
linkedProgram->getEntryPointCode(1, 0, closestHitCode.writeRef(), diagnostics.writeRef()); linkedProgram->getEntryPointCode(1, 0, closestHitCode.writeRef(), diagnostics.writeRef());
Slang::ComPtr<IBlob> missCode;
linkedProgram->getEntryPointCode(2, 0, missCode.writeRef(), diagnostics.writeRef());
rayGen = rayGen =
ShaderModule(device, vk::ShaderModuleCreateInfo({}, rayGenCode->getBufferSize(), (const uint32_t*)rayGenCode->getBufferPointer())); ShaderModule(device, vk::ShaderModuleCreateInfo({}, rayGenCode->getBufferSize(), (const uint32_t*)rayGenCode->getBufferPointer()));
closestHit = ShaderModule( closestHit = ShaderModule(
device, vk::ShaderModuleCreateInfo({}, closestHitCode->getBufferSize(), (const uint32_t*)closestHitCode->getBufferPointer())); device, vk::ShaderModuleCreateInfo({}, closestHitCode->getBufferSize(), (const uint32_t*)closestHitCode->getBufferPointer()));
std::vector<VkPipelineShaderStageCreateInfo> shaderStages; miss = ShaderModule(device, vk::ShaderModuleCreateInfo({}, missCode->getBufferSize(), (const uint32_t*)missCode->getBufferPointer()));
std::vector<VkRayTracingShaderGroupCreateInfoKHR> shaderGroups;
std::vector<vk::PipelineShaderStageCreateInfo> shaderStages;
std::vector<vk::RayTracingShaderGroupCreateInfoKHR> shaderGroups;
{ {
shaderStages.push_back(VkPipelineShaderStageCreateInfo{ shaderStages.push_back(vk::PipelineShaderStageCreateInfo({}, vk::ShaderStageFlagBits::eRaygenKHR, rayGen, "main"));
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO, shaderGroups.push_back(vk::RayTracingShaderGroupCreateInfoKHR(vk::RayTracingShaderGroupTypeKHR::eGeneral, shaderStages.size() - 1,
.pNext = nullptr, vk::ShaderUnusedKHR, vk::ShaderUnusedKHR, vk::ShaderUnusedKHR));
.flags = 0,
.stage = VK_SHADER_STAGE_RAYGEN_BIT_KHR,
.module = *rayGen,
.pName = "rayGen",
.pSpecializationInfo = nullptr,
});
shaderGroups.push_back(VkRayTracingShaderGroupCreateInfoKHR{
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR,
.pNext = nullptr,
.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_GENERAL_KHR,
.generalShader = static_cast<uint32_t>(shaderStages.size() - 1),
.closestHitShader = VK_SHADER_UNUSED_KHR,
.anyHitShader = VK_SHADER_UNUSED_KHR,
.intersectionShader = VK_SHADER_UNUSED_KHR,
.pShaderGroupCaptureReplayHandle = nullptr,
});
} }
{ {
shaderStages.push_back(VkPipelineShaderStageCreateInfo{ shaderStages.push_back(vk::PipelineShaderStageCreateInfo({}, vk::ShaderStageFlagBits::eClosestHitKHR, closestHit, "main"));
.sType = VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.stage = VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR,
.module = *closestHit,
.pName = "closestHit",
.pSpecializationInfo = nullptr,
});
uint32_t hitIndex = static_cast<uint32_t>(shaderStages.size() - 1); uint32_t hitIndex = static_cast<uint32_t>(shaderStages.size() - 1);
uint32_t anyHitIndex = VK_SHADER_UNUSED_KHR; uint32_t anyHitIndex = VK_SHADER_UNUSED_KHR;
uint32_t intersectionIndex = VK_SHADER_UNUSED_KHR; uint32_t intersectionIndex = VK_SHADER_UNUSED_KHR;
@@ -250,21 +258,118 @@ void GPURenderer::createShaders()
// .pSpecializationInfo = nullptr, // .pSpecializationInfo = nullptr,
// }); // });
// } // }
shaderGroups.push_back(VkRayTracingShaderGroupCreateInfoKHR{ shaderGroups.push_back(vk::RayTracingShaderGroupCreateInfoKHR(vk::RayTracingShaderGroupTypeKHR::eTrianglesHitGroup, vk::ShaderUnusedKHR,
.sType = VK_STRUCTURE_TYPE_RAY_TRACING_SHADER_GROUP_CREATE_INFO_KHR, hitIndex, anyHitIndex, intersectionIndex));
.pNext = nullptr,
.type = VK_RAY_TRACING_SHADER_GROUP_TYPE_TRIANGLES_HIT_GROUP_KHR,
.generalShader = VK_SHADER_UNUSED_KHR,
.closestHitShader = hitIndex,
.anyHitShader = anyHitIndex,
.intersectionShader = intersectionIndex,
.pShaderGroupCaptureReplayHandle = nullptr,
});
} }
{
shaderStages.push_back(vk::PipelineShaderStageCreateInfo({}, vk::ShaderStageFlagBits::eMissKHR, miss, "main"));
shaderGroups.push_back(vk::RayTracingShaderGroupCreateInfoKHR(vk::RayTracingShaderGroupTypeKHR::eGeneral, shaderStages.size() - 1,
vk::ShaderUnusedKHR, vk::ShaderUnusedKHR, vk::ShaderUnusedKHR));
}
pipeline = device.createRayTracingPipelineKHR(
nullptr, nullptr, vk::RayTracingPipelineCreateInfoKHR({}, shaderStages, shaderGroups, 12, nullptr, nullptr, nullptr, pipelineLayout));
const uint32_t handleSize = rayTracingProperties.shaderGroupHandleSize;
const uint32_t handleSizeAligned = align(rayTracingProperties.shaderGroupHandleSize, rayTracingProperties.shaderGroupHandleAlignment);
const uint32_t handleAlignment = rayTracingProperties.shaderGroupHandleAlignment;
const uint32_t sbtAlignment = rayTracingProperties.shaderGroupBaseAlignment;
const uint32_t groupCount = static_cast<uint32_t>(shaderGroups.size());
const uint32_t sbtSize = groupCount * handleSizeAligned;
const VkBufferUsageFlags sbtBufferUsage =
VK_BUFFER_USAGE_SHADER_BINDING_TABLE_BIT_KHR | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
const VmaMemoryUsage sbtMemoryUsage = VMA_MEMORY_USAGE_AUTO;
uint64_t rayGenStride = handleSize;
uint64_t hitStride = handleSize;
uint64_t missStride = handleSize;
auto rayGenSBTInfo = VkBufferCreateInfo{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.size = rayGenStride,
.usage = sbtBufferUsage,
};
auto rayGenSBTAllocInfo = VmaAllocationCreateInfo{
.usage = sbtMemoryUsage,
};
VkBuffer rayGenSBTBuf;
vmaCreateBufferWithAlignment(allocator, &rayGenSBTInfo, &rayGenSBTAllocInfo, sbtAlignment, &rayGenSBTBuf, &rayGenAlloc, nullptr);
rayGenSBT = Buffer(device, rayGenSBTBuf);
rayGenAddr =
vk::StridedDeviceAddressRegionKHR(device.getBufferAddress(vk::BufferDeviceAddressInfo(*rayGenSBT)), rayGenStride, rayGenStride);
auto closestHitSBTInfo = VkBufferCreateInfo{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.size = hitStride,
.usage = sbtBufferUsage,
};
auto closestHitSBTAllocInfo = VmaAllocationCreateInfo{
.usage = sbtMemoryUsage,
};
VkBuffer closestHitSBTBuf;
vmaCreateBufferWithAlignment(allocator, &closestHitSBTInfo, &closestHitSBTAllocInfo, sbtAlignment, &closestHitSBTBuf, &closestHitAlloc,
nullptr);
closestHitSBT = Buffer(device, closestHitSBTBuf);
closestHitAddr =
vk::StridedDeviceAddressRegionKHR(device.getBufferAddress(vk::BufferDeviceAddressInfo(*closestHitSBT)), hitStride, hitStride);
auto missSBTInfo = VkBufferCreateInfo{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.size = missStride,
.usage = sbtBufferUsage,
};
auto missSBTAllocInfo = VmaAllocationCreateInfo{
.usage = sbtMemoryUsage,
};
VkBuffer missSBTBuf;
vmaCreateBufferWithAlignment(allocator, &missSBTInfo, &missSBTAllocInfo, sbtAlignment, &missSBTBuf, &missAlloc, nullptr);
missSBT = Buffer(device, missSBTBuf);
missAddr = vk::StridedDeviceAddressRegionKHR(device.getBufferAddress(vk::BufferDeviceAddressInfo(*missSBT)), missStride, missStride);
std::vector<unsigned char> sbt = pipeline.getRayTracingShaderGroupHandlesKHR<unsigned char>(0, shaderGroups.size(), sbtSize);
uploadToGPU(rayGenSBT, sbt.data(), rayGenStride);
uploadToGPU(closestHitSBT, sbt.data() + handleSize, handleSize);
uploadToGPU(missSBT, sbt.data() + handleSize * 2, handleSize);
}
void GPURenderer::uploadToGPU(Buffer& buffer, void* data, size_t size)
{
VkBufferCreateInfo stagingBufInfo = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = size,
.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
};
VmaAllocationCreateInfo stagingAllocInfo = {
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT,
.usage = VMA_MEMORY_USAGE_AUTO,
};
VkBuffer stagingBuf;
VmaAllocation stagingAllocation;
vmaCreateBuffer(allocator, &stagingBufInfo, &stagingAllocInfo, &stagingBuf, &stagingAllocation, nullptr);
Buffer stagingBuffer = Buffer(device, stagingBuf);
vmaCopyMemoryToAllocation(allocator, data, stagingAllocation, 0, size);
CommandBuffer copyCmd =
std::move(device.allocateCommandBuffers(vk::CommandBufferAllocateInfo(cmdPool, vk::CommandBufferLevel::ePrimary, 1)).front());
copyCmd.begin(vk::CommandBufferBeginInfo(vk::CommandBufferUsageFlagBits::eOneTimeSubmit));
copyCmd.copyBuffer(stagingBuffer, buffer, vk::BufferCopy(0, 0, size));
copyCmd.end();
queue.submit(vk::SubmitInfo({}, {}, *copyCmd, {}));
device.waitIdle();
} }
void GPURenderer::render(Camera cam, RenderParameter param) void GPURenderer::render(Camera cam, RenderParameter param)
{ {
for (uint32_t samp = 0; samp < param.numSamples; ++samp)
{
semaphores.push_back(device.createSemaphore(vk::SemaphoreCreateInfo()));
fences.push_back(device.createFence(vk::FenceCreateInfo()));
}
// camera // camera
{ {
VkBufferCreateInfo bufferInfo = { VkBufferCreateInfo bufferInfo = {
@@ -277,7 +382,9 @@ void GPURenderer::render(Camera cam, RenderParameter param)
.usage = VMA_MEMORY_USAGE_AUTO, .usage = VMA_MEMORY_USAGE_AUTO,
}; };
vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &cameraBuffer, &cameraAllocation, nullptr); VkBuffer camBuf;
vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &camBuf, &cameraAllocation, nullptr);
cameraBuffer = Buffer(device, camBuf);
GPUCamera gpuCam = { GPUCamera gpuCam = {
.cameraPosition = cam.position, .cameraPosition = cam.position,
.f = cam.f, .f = cam.f,
@@ -288,7 +395,7 @@ void GPURenderer::render(Camera cam, RenderParameter param)
.A = cam.A, .A = cam.A,
.ka = 0, .ka = 0,
}; };
vmaCopyMemoryToAllocation(allocator, &gpuCam, cameraAllocation, 0, sizeof(GPUCamera)); uploadToGPU(cameraBuffer, &gpuCam, sizeof(GPUCamera));
} }
// radiance accumulator // radiance accumulator
{ {
@@ -317,7 +424,8 @@ void GPURenderer::render(Camera cam, RenderParameter param)
VkImage radianceImg; VkImage radianceImg;
vmaCreateImage(allocator, &imageInfo, &allocCreateInfo, &radianceImg, &radianceAllocation, nullptr); vmaCreateImage(allocator, &imageInfo, &allocCreateInfo, &radianceImg, &radianceAllocation, nullptr);
radianceAccumulator = Image(device, radianceImg); radianceAccumulator = Image(device, radianceImg);
radianceView = device.createImageView(vk::ImageViewCreateInfo({}, *radianceAccumulator, vk::ImageViewType::e2D, vk::Format::eR32G32B32A32Sfloat)); radianceView =
device.createImageView(vk::ImageViewCreateInfo({}, *radianceAccumulator, vk::ImageViewType::e2D, vk::Format::eR32G32B32A32Sfloat));
} }
// image // image
{ {
@@ -349,7 +457,8 @@ void GPURenderer::render(Camera cam, RenderParameter param)
radianceView = device.createImageView(vk::ImageViewCreateInfo({}, *image, vk::ImageViewType::e2D, vk::Format::eR32G32B32A32Sfloat)); radianceView = device.createImageView(vk::ImageViewCreateInfo({}, *image, vk::ImageViewType::e2D, vk::Format::eR32G32B32A32Sfloat));
} }
DescriptorSet descriptorSet = std::move(device.allocateDescriptorSets(vk::DescriptorSetAllocateInfo(*descriptorPool, *descriptorLayout)).front()); DescriptorSet descriptorSet =
std::move(device.allocateDescriptorSets(vk::DescriptorSetAllocateInfo(*descriptorPool, *descriptorLayout)).front());
std::vector<vk::WriteDescriptorSet> writes; std::vector<vk::WriteDescriptorSet> writes;
// have to use lists so the pointers arent invalidated by push // have to use lists so the pointers arent invalidated by push
std::list<vk::DescriptorBufferInfo> buffers; std::list<vk::DescriptorBufferInfo> buffers;
@@ -358,61 +467,66 @@ void GPURenderer::render(Camera cam, RenderParameter param)
uint32_t bindingCounter = 0; uint32_t bindingCounter = 0;
{ {
buffers.push_back(vk::DescriptorBufferInfo(cameraBuffer)); buffers.push_back(vk::DescriptorBufferInfo(cameraBuffer));
writes.push_back(vk::WriteDescriptorSet(*descriptorSet, bindingCounter++, 0, 1, vk::DescriptorType::eUniformBuffer, nullptr, &buffers.back(), nullptr)); writes.push_back(vk::WriteDescriptorSet(*descriptorSet, bindingCounter++, 0, 1, vk::DescriptorType::eUniformBuffer, nullptr,
&buffers.back(), nullptr));
} }
{ {
accel.push_back(vk::WriteDescriptorSetAccelerationStructureKHR(1, scene->accelerationStructure)); accel.push_back(vk::WriteDescriptorSetAccelerationStructureKHR(*((GPUScene*)scene.get())->accelerationStructure));
writes.push_back(vk::WriteDescriptorSet(*descriptorSet, bindingCounter++, 0, 1, vk::DescriptorType::eAccelerationStructureKHR, nullptr, nullptr, nullptr, &accel.back())); writes.push_back(vk::WriteDescriptorSet(*descriptorSet, bindingCounter++, 0, 1, vk::DescriptorType::eAccelerationStructureKHR, nullptr,
nullptr, nullptr, &accel.back()));
} }
{ {
images.push_back(vk::DescriptorImageInfo({}, radianceView, vk::ImageLayout::eGeneral)); images.push_back(vk::DescriptorImageInfo({}, radianceView, vk::ImageLayout::eGeneral));
writes.push_back(vk::WriteDescriptorSet(*descriptorSet, bindingCounter++, 0, 1, vk::DescriptorType::eStorageImage, &images.back(), nullptr, nullptr)); writes.push_back(vk::WriteDescriptorSet(*descriptorSet, bindingCounter++, 0, 1, vk::DescriptorType::eStorageImage, &images.back(),
nullptr, nullptr));
} }
{ {
images.push_back(vk::DescriptorImageInfo({}, imageView, vk::ImageLayout::eGeneral)); images.push_back(vk::DescriptorImageInfo({}, imageView, vk::ImageLayout::eGeneral));
writes.push_back(vk::WriteDescriptorSet(*descriptorSet, bindingCounter++, 0, 1, vk::DescriptorType::eStorageImage, &images.back(), nullptr, nullptr)); writes.push_back(vk::WriteDescriptorSet(*descriptorSet, bindingCounter++, 0, 1, vk::DescriptorType::eStorageImage, &images.back(),
nullptr, nullptr));
} }
{ {
buffers.push_back(vk::DescriptorBufferInfo(scene->modelBuffer)); buffers.push_back(vk::DescriptorBufferInfo(((GPUScene*)scene.get())->modelBuffer));
writes.push_back(vk::WriteDescriptorSet(*descriptorSet, bindingCounter++, 0, 1, vk::DescriptorType::eStorageBuffer, nullptr, &buffers.back(), nullptr)); writes.push_back(vk::WriteDescriptorSet(*descriptorSet, bindingCounter++, 0, 1, vk::DescriptorType::eStorageBuffer, nullptr,
&buffers.back(), nullptr));
} }
{ {
buffers.push_back(vk::DescriptorBufferInfo(scene->materialBuffer)); buffers.push_back(vk::DescriptorBufferInfo(((GPUScene*)scene.get())->materialBuffer));
writes.push_back(vk::WriteDescriptorSet(*descriptorSet, bindingCounter++, 0, 1, vk::DescriptorType::eStorageBuffer, nullptr, &buffers.back(), nullptr)); writes.push_back(vk::WriteDescriptorSet(*descriptorSet, bindingCounter++, 0, 1, vk::DescriptorType::eStorageBuffer, nullptr,
&buffers.back(), nullptr));
} }
{ {
buffers.push_back(vk::DescriptorBufferInfo(scene->positionsBuffer)); buffers.push_back(vk::DescriptorBufferInfo(((GPUScene*)scene.get())->positionBuffer));
writes.push_back(vk::WriteDescriptorSet(*descriptorSet, bindingCounter++, 0, 1, vk::DescriptorType::eStorageBuffer, nullptr, &buffers.back(), nullptr)); writes.push_back(vk::WriteDescriptorSet(*descriptorSet, bindingCounter++, 0, 1, vk::DescriptorType::eStorageBuffer, nullptr,
&buffers.back(), nullptr));
} }
{ {
buffers.push_back(vk::DescriptorBufferInfo(scene->texCoordsBuffer)); buffers.push_back(vk::DescriptorBufferInfo(((GPUScene*)scene.get())->texCoordsBuffer));
writes.push_back(vk::WriteDescriptorSet(*descriptorSet, bindingCounter++, 0, 1, vk::DescriptorType::eStorageBuffer, nullptr, &buffers.back(), nullptr)); writes.push_back(vk::WriteDescriptorSet(*descriptorSet, bindingCounter++, 0, 1, vk::DescriptorType::eStorageBuffer, nullptr,
&buffers.back(), nullptr));
} }
{ {
buffers.push_back(vk::DescriptorBufferInfo(scene->normalsBuffer)); buffers.push_back(vk::DescriptorBufferInfo(((GPUScene*)scene.get())->normalsBuffer));
writes.push_back(vk::WriteDescriptorSet(*descriptorSet, bindingCounter++, 0, 1, vk::DescriptorType::eStorageBuffer, nullptr, &buffers.back(), nullptr)); writes.push_back(vk::WriteDescriptorSet(*descriptorSet, bindingCounter++, 0, 1, vk::DescriptorType::eStorageBuffer, nullptr,
&buffers.back(), nullptr));
} }
{ {
buffers.push_back(vk::DescriptorBufferInfo(scene->directionalLightsBuffer)); buffers.push_back(vk::DescriptorBufferInfo(((GPUScene*)scene.get())->directionalLightBuffer));
writes.push_back(vk::WriteDescriptorSet(*descriptorSet, bindingCounter++, 0, 1, vk::DescriptorType::eStorageBuffer, nullptr, &buffers.back(), nullptr)); writes.push_back(vk::WriteDescriptorSet(*descriptorSet, bindingCounter++, 0, 1, vk::DescriptorType::eStorageBuffer, nullptr,
&buffers.back(), nullptr));
} }
{ {
buffers.push_back(vk::DescriptorBufferInfo(scene->pointLightsBuffer)); buffers.push_back(vk::DescriptorBufferInfo(((GPUScene*)scene.get())->pointLightBuffer));
writes.push_back(vk::WriteDescriptorSet(*descriptorSet, bindingCounter++, 0, 1, vk::DescriptorType::eStorageBuffer, nullptr, &buffers.back(), nullptr)); writes.push_back(vk::WriteDescriptorSet(*descriptorSet, bindingCounter++, 0, 1, vk::DescriptorType::eStorageBuffer, nullptr,
&buffers.back(), nullptr));
} }
{ {
buffers.push_back(vk::DescriptorBufferInfo(scene->indexBuffer)); buffers.push_back(vk::DescriptorBufferInfo(((GPUScene*)scene.get())->indexBuffer));
writes.push_back(vk::WriteDescriptorSet(*descriptorSet, bindingCounter++, 0, 1, vk::DescriptorType::eStorageBuffer, nullptr, &buffers.back(), nullptr)); writes.push_back(vk::WriteDescriptorSet(*descriptorSet, bindingCounter++, 0, 1, vk::DescriptorType::eStorageBuffer, nullptr,
&buffers.back(), nullptr));
} }
device.updateDescriptorSets(writes, {}); device.updateDescriptorSets(writes, {});
semaphores.resize(param.numSamples);
fences.resize(param.numSamples);
for (uint32_t samp = 0; samp < param.numSamples; ++samp)
{
semaphores[samp] = device.createSemaphore(vk::SemaphoreCreateInfo());
fences[samp] = device.createFence(vk::FenceCreateInfo());
}
// allocate a CommandBuffer from the CommandPool // allocate a CommandBuffer from the CommandPool
vk::CommandBufferAllocateInfo commandBufferAllocateInfo(*cmdPool, vk::CommandBufferLevel::ePrimary, param.numSamples); vk::CommandBufferAllocateInfo commandBufferAllocateInfo(*cmdPool, vk::CommandBufferLevel::ePrimary, param.numSamples);
cmdBuffers = CommandBuffers(device, commandBufferAllocateInfo); cmdBuffers = CommandBuffers(device, commandBufferAllocateInfo);
@@ -421,20 +535,29 @@ void GPURenderer::render(Camera cam, RenderParameter param)
auto& cmd = cmdBuffers[samp]; auto& cmd = cmdBuffers[samp];
cmd.begin(vk::CommandBufferBeginInfo(vk::CommandBufferUsageFlagBits::eOneTimeSubmit)); cmd.begin(vk::CommandBufferBeginInfo(vk::CommandBufferUsageFlagBits::eOneTimeSubmit));
cmd.bindPipeline(vk::PipelineBindPoint::eRayTracingKHR, *pipeline); cmd.bindPipeline(vk::PipelineBindPoint::eRayTracingKHR, *pipeline);
cmd.bindDescriptorSets(vk::PipelineBindPoint::eRayTracingKHR, pipelineLayout, 0, descriptorSet, {}); cmd.bindDescriptorSets(vk::PipelineBindPoint::eRayTracingKHR, pipelineLayout, 0, *descriptorSet, {});
cmd.traceRays(param.width, param.height, 1); std::vector<SampleParams> sampleParams = {SampleParams{
.pass = samp,
.samplesPerPixel = param.numSamples,
.numDirectionalLights = (uint32_t)scene->directionalLights.size(),
.numPointLights = (uint32_t)scene->pointLights.size(),
}};
cmd.pushConstants<SampleParams>(pipelineLayout, vk::ShaderStageFlagBits::eRaygenKHR | vk::ShaderStageFlagBits::eClosestHitKHR, 0,
sampleParams);
cmd.traceRaysKHR(rayGenAddr, closestHitAddr, missAddr, {}, param.width, param.height, 1);
cmd.end(); cmd.end();
if (samp == 0) if (samp == 0)
{ {
queue.submit(vk::SubmitInfo({}, cmd, semaphores[samp]), fences[samp]); queue.submit(vk::SubmitInfo({}, {}, *cmd, *semaphores[samp]), *fences[samp]);
} }
else else
{ {
queue.submit(vk::SubmitInfo(semaphores[samp-1], vk::PipelineStageFlagBits::eRayTracingShaderKHR, cmd, semaphores[samp]), fences[samp]); vk::PipelineStageFlags dstWaitMask = vk::PipelineStageFlagBits::eRayTracingShaderKHR;
queue.submit(vk::SubmitInfo(*semaphores[samp - 1], dstWaitMask, *cmd, *semaphores[samp]), *fences[samp]);
} }
} }
for (uint32_t samp = 0; samp < param.numSamples; ++samp) for (uint32_t samp = 0; samp < param.numSamples; ++samp)
{ {
device.waitForFences(fences[samp], true, 1000000); assert(device.waitForFences(*fences[samp], true, 1000000) == vk::Result::eSuccess);
} }
} }
+47 -25
View File
@@ -12,6 +12,7 @@ struct GPURenderer : public Renderer
public: public:
GPURenderer(); GPURenderer();
virtual ~GPURenderer(); virtual ~GPURenderer();
virtual void render(Camera cam, RenderParameter param) override;
private: private:
struct GPUCamera struct GPUCamera
@@ -25,47 +26,68 @@ private:
float A; float A;
float ka; float ka;
}; };
struct SampleParams
{
uint32_t pass;
uint32_t samplesPerPixel;
uint32_t numDirectionalLights;
uint32_t numPointLights;
};
void createDevice(); void createDevice();
void createCommands(); void createCommands();
void createDescriptors(); void createDescriptors();
void createShaders(); void createPipeline();
std::unique_ptr<GPUScene> scene;
Context context; Context context;
Instance instance; Instance instance = nullptr;
PhysicalDevice physicalDevice; PhysicalDevice physicalDevice = nullptr;
Device device; Device device = nullptr;
Queue queue; Queue queue = nullptr;
VmaAllocator allocator; VmaAllocator allocator = nullptr;
uint32_t computeQueueFamily; vk::PhysicalDeviceAccelerationStructurePropertiesKHR accelerationProperties = {};
CommandPool cmdPool; vk::PhysicalDeviceRayTracingPipelinePropertiesKHR rayTracingProperties = {};
CommandBuffers cmdBuffers;
uint32_t computeQueueFamily = 0;
CommandPool cmdPool = nullptr;
CommandBuffers cmdBuffers = nullptr;
std::vector<Semaphore> semaphores; std::vector<Semaphore> semaphores;
std::vector<Fence> fences; std::vector<Fence> fences;
DescriptorSetLayout descriptorLayout; DescriptorSetLayout descriptorLayout = nullptr;
DescriptorSet descriptorSet; DescriptorSet descriptorSet = nullptr;
DescriptorPool descriptorPool; DescriptorPool descriptorPool = nullptr;
PipelineLayout pipelineLayout; PipelineLayout pipelineLayout = nullptr;
ShaderModule rayGen; ShaderModule rayGen = nullptr;
ShaderModule closestHit; ShaderModule closestHit = nullptr;
ShaderModule miss; ShaderModule miss = nullptr;
Pipeline pipeline; Pipeline pipeline = nullptr;
Buffer cameraBuffer; Buffer rayGenSBT = nullptr;
vk::StridedDeviceAddressRegionKHR rayGenAddr;
VmaAllocation rayGenAlloc;
Buffer closestHitSBT = nullptr;
vk::StridedDeviceAddressRegionKHR closestHitAddr;
VmaAllocation closestHitAlloc;
Buffer missSBT = nullptr;
vk::StridedDeviceAddressRegionKHR missAddr;
VmaAllocation missAlloc;
Buffer cameraBuffer = nullptr;
VmaAllocation cameraAllocation; VmaAllocation cameraAllocation;
Image radianceAccumulator; Image radianceAccumulator = nullptr;
ImageView radianceView; ImageView radianceView = nullptr;
VmaAllocation radianceAllocation; VmaAllocation radianceAllocation;
Image image; Image image = nullptr;
ImageView imageView; ImageView imageView = nullptr;
VmaAllocation imageAllocation; VmaAllocation imageAllocation;
virtual void render(Camera cam, RenderParameter param); void uploadToGPU(Buffer& buffer, void* data, size_t size);
}; };
+110 -22
View File
@@ -1,11 +1,14 @@
#include "GPUScene.h" #include "GPUScene.h"
GPUScene::GPUScene(Device& device, VmaAllocator& allocator, CommandPool& cmdPool, Queue& queue)
: device(device), allocator(allocator), cmdPool(cmdPool), queue(queue)
{
}
GPUScene::~GPUScene() {} GPUScene::~GPUScene() {}
void GPUScene::generate() void GPUScene::createRayTracingHierarchy()
{ {
populateGeometryPools();
// upload geometry to gpu // upload geometry to gpu
createStorageBuffer(modelBuffer, modelAllocation, refs.data(), refs.size() * sizeof(ModelReference)); createStorageBuffer(modelBuffer, modelAllocation, refs.data(), refs.size() * sizeof(ModelReference));
// createStorageBuffer(materialBuffer, materialAllocation, refs.data(), refs.size() * sizeof(ModelReference)); // createStorageBuffer(materialBuffer, materialAllocation, refs.data(), refs.size() * sizeof(ModelReference));
@@ -17,14 +20,11 @@ void GPUScene::generate()
createStorageBuffer(pointLightBuffer, pointLightAllocation, pointLights.data(), pointLights.size() * sizeof(PointLight)); createStorageBuffer(pointLightBuffer, pointLightAllocation, pointLights.data(), pointLights.size() * sizeof(PointLight));
createStorageBuffer(indexBuffer, indexAllocation, indicesPool.data(), indicesPool.size() * sizeof(glm::uvec3)); createStorageBuffer(indexBuffer, indexAllocation, indicesPool.data(), indicesPool.size() * sizeof(glm::uvec3));
VkBufferDeviceAddressInfo addrInfo = { vk::DeviceAddress vertexBufferAddr = device.getBufferAddress(vk::BufferDeviceAddressInfo(positionBuffer));
.sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO, vk::DeviceAddress indexBufferAddr = device.getBufferAddress(vk::BufferDeviceAddressInfo(indexBuffer));
.buffer = positionBuffer,
};
VkDeviceAddress vertexBufferAddr = vkGetBufferDeviceAddress(*device, &addrInfo);
addrInfo.buffer = indexBuffer;
VkDeviceAddress indexBufferAddr = vkGetBufferDeviceAddress(*device, &addrInfo);
std::vector<vk::AccelerationStructureInstanceKHR> instances(models.size());
{
std::vector<vk::AccelerationStructureGeometryKHR> geometries(models.size()); std::vector<vk::AccelerationStructureGeometryKHR> geometries(models.size());
std::vector<vk::AccelerationStructureBuildGeometryInfoKHR> buildGeometries(models.size()); std::vector<vk::AccelerationStructureBuildGeometryInfoKHR> buildGeometries(models.size());
std::vector<vk::AccelerationStructureBuildSizesInfoKHR> buildSizes(models.size()); std::vector<vk::AccelerationStructureBuildSizesInfoKHR> buildSizes(models.size());
@@ -45,7 +45,7 @@ void GPUScene::generate()
vk::GeometryFlagBitsKHR::eOpaque); vk::GeometryFlagBitsKHR::eOpaque);
buildGeometries[i] = vk::AccelerationStructureBuildGeometryInfoKHR( buildGeometries[i] = vk::AccelerationStructureBuildGeometryInfoKHR(
vk::AccelerationStructureTypeKHR::eTopLevel, vk::BuildAccelerationStructureFlagBitsKHR::ePreferFastTrace, vk::AccelerationStructureTypeKHR::eBottomLevel, vk::BuildAccelerationStructureFlagBitsKHR::ePreferFastTrace,
vk::BuildAccelerationStructureModeKHR::eBuild, {}, {}, 1, &geometries[i], nullptr); vk::BuildAccelerationStructureModeKHR::eBuild, {}, {}, 1, &geometries[i], nullptr);
buildSizes[i] = { buildSizes[i] = {
@@ -66,7 +66,9 @@ void GPUScene::generate()
VmaAllocationCreateInfo bufferAllocInfo = { VmaAllocationCreateInfo bufferAllocInfo = {
.usage = VMA_MEMORY_USAGE_AUTO, .usage = VMA_MEMORY_USAGE_AUTO,
}; };
vmaCreateBuffer(allocator, &bufferInfo, &bufferAllocInfo, &blas[i].buffer, &blas[i].alloc, nullptr); VkBuffer buf;
vmaCreateBuffer(allocator, &bufferInfo, &bufferAllocInfo, &buf, &blas[i].alloc, nullptr);
blas[i].buffer = Buffer(device, buf);
vk::AccelerationStructureCreateInfoKHR blasInfo({}, blas[i].buffer, 0, buildSizes[i].accelerationStructureSize, vk::AccelerationStructureCreateInfoKHR blasInfo({}, blas[i].buffer, 0, buildSizes[i].accelerationStructureSize,
vk::AccelerationStructureTypeKHR::eBottomLevel); vk::AccelerationStructureTypeKHR::eBottomLevel);
@@ -83,8 +85,7 @@ void GPUScene::generate()
.usage = VMA_MEMORY_USAGE_AUTO, .usage = VMA_MEMORY_USAGE_AUTO,
}; };
vmaCreateBufferWithAlignment(allocator, &scratchInfo, &scratchAllocInfo, 16, &scratchBuffers[i], &scratchAllocations[i], nullptr); vmaCreateBufferWithAlignment(allocator, &scratchInfo, &scratchAllocInfo, 16, &scratchBuffers[i], &scratchAllocations[i], nullptr);
addrInfo.buffer = scratchBuffers[i]; vk::DeviceAddress scratchAddr = device.getBufferAddress(vk::BufferDeviceAddressInfo(scratchBuffers[i]));
VkDeviceAddress scratchAddr = vkGetBufferDeviceAddress(*device, &addrInfo);
buildGeometries[i].dstAccelerationStructure = blas[i].handle; buildGeometries[i].dstAccelerationStructure = blas[i].handle;
buildGeometries[i].scratchData.deviceAddress = scratchAddr; buildGeometries[i].scratchData.deviceAddress = scratchAddr;
@@ -95,30 +96,117 @@ void GPUScene::generate()
.transformOffset = 0, .transformOffset = 0,
}; };
buildRangePointers[i] = &buildRanges[i]; buildRangePointers[i] = &buildRanges[i];
vk::DeviceAddress blasAddr = device.getBufferAddress(vk::BufferDeviceAddressInfo(blas[i].buffer));
instances[i] = vk::AccelerationStructureInstanceKHR({}, i, 0xff, 0, {}, blasAddr);
} }
vk::CommandBufferAllocateInfo commandBufferAllocateInfo(*cmdPool, vk::CommandBufferLevel::ePrimary, 10); vk::CommandBufferAllocateInfo commandBufferAllocateInfo(*cmdPool, vk::CommandBufferLevel::ePrimary, 10);
CommandBuffer cmdBuffer = std::move(CommandBuffers(device, commandBufferAllocateInfo).front()); CommandBuffer cmdBuffer = std::move(CommandBuffers(device, commandBufferAllocateInfo).front());
vk::FenceCreateInfo fenceCreateInfo;
Fence fence = Fence(device, fenceCreateInfo);
cmdBuffer.begin(vk::CommandBufferBeginInfo(vk::CommandBufferUsageFlagBits::eOneTimeSubmit)); cmdBuffer.begin(vk::CommandBufferBeginInfo(vk::CommandBufferUsageFlagBits::eOneTimeSubmit));
cmdBuffer.buildAccelerationStructuresKHR(buildGeometries, buildRangePointers); cmdBuffer.buildAccelerationStructuresKHR(buildGeometries, buildRangePointers);
cmdBuffer.end(); cmdBuffer.end();
vk::SubmitInfo submitInfo; vk::SubmitInfo submitInfo;
queue.submit(submitInfo, *fence); queue.submit(submitInfo);
assert(device.waitForFences({*fence}, true, 1000000) == vk::Result::eSuccess); device.waitIdle();
}
createStorageBuffer(instanceBuffer, instanceAllocation, instances.data(), instances.size());
vk::DeviceAddress instancesAddress = device.getBufferAddress(vk::BufferDeviceAddressInfo(instanceBuffer));
vk::AccelerationStructureGeometryKHR geometry(vk::GeometryTypeKHR::eInstances,
vk::AccelerationStructureGeometryInstancesDataKHR(false, {instancesAddress}),
vk::GeometryFlagBitsKHR::eOpaque);
vk::AccelerationStructureBuildGeometryInfoKHR structureBuildGeometry(vk::AccelerationStructureTypeKHR::eTopLevel,
vk::BuildAccelerationStructureFlagBitsKHR::ePreferFastTrace,
vk::BuildAccelerationStructureModeKHR::eBuild, {}, {}, geometry);
const uint32_t primitiveCount = instances.size();
auto buildSizes =
device.getAccelerationStructureBuildSizesKHR(vk::AccelerationStructureBuildTypeKHR::eDevice, structureBuildGeometry, primitiveCount);
VkBuffer buffer;
auto tlasInfo = VkBufferCreateInfo{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.size = buildSizes.accelerationStructureSize,
.usage = VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT,
};
auto tlasAlloc = VmaAllocationCreateInfo{
.usage = VMA_MEMORY_USAGE_AUTO,
};
vmaCreateBuffer(allocator, &tlasInfo, &tlasAlloc, &buffer, &accelerationAllocation, nullptr);
accelerationBuffer = Buffer(device, buffer);
accelerationStructure = device.createAccelerationStructureKHR(vk::AccelerationStructureCreateInfoKHR(
{}, accelerationBuffer, 0, buildSizes.accelerationStructureSize, vk::AccelerationStructureTypeKHR::eTopLevel));
auto scratchInfo = VkBufferCreateInfo{
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.size = buildSizes.buildScratchSize,
.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT,
};
auto scratchAllocInfo = VmaAllocationCreateInfo{
.usage = VMA_MEMORY_USAGE_AUTO,
};
VkBuffer scratchBuf;
VmaAllocation scratchAlloc;
vmaCreateBufferWithAlignment(allocator, &scratchInfo, &scratchAllocInfo, 64, &scratchBuf, &scratchAlloc, nullptr);
Buffer scratchBuffer = Buffer(device, scratchBuf);
vk::DeviceAddress scratchAddr = device.getBufferAddress(vk::BufferDeviceAddressInfo(scratchBuffer));
vk::AccelerationStructureBuildGeometryInfoKHR buildGeometry(
vk::AccelerationStructureTypeKHR::eTopLevel, vk::BuildAccelerationStructureFlagBitsKHR::ePreferFastTrace,
vk::BuildAccelerationStructureModeKHR::eBuild, {}, accelerationStructure, geometry, {}, {scratchAddr});
vk::AccelerationStructureBuildRangeInfoKHR buildRange(primitiveCount, 0, 0, 0);
vk::CommandBufferAllocateInfo commandBufferAllocateInfo(*cmdPool, vk::CommandBufferLevel::ePrimary, 10);
CommandBuffer cmdBuffer = std::move(CommandBuffers(device, commandBufferAllocateInfo).front());
cmdBuffer.begin(vk::CommandBufferBeginInfo(vk::CommandBufferUsageFlagBits::eOneTimeSubmit));
cmdBuffer.buildAccelerationStructuresKHR(buildGeometry, {&buildRange});
cmdBuffer.end();
vk::SubmitInfo submitInfo;
queue.submit(submitInfo);
device.waitIdle();
} }
void GPUScene::createStorageBuffer(VkBuffer& buffer, VmaAllocation& alloc, void* data, size_t size) void GPUScene::createStorageBuffer(Buffer& buffer, VmaAllocation& alloc, void* data, size_t size)
{ {
if (size == 0)
return;
VkBufferCreateInfo bufferCreateInfo = {VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; VkBufferCreateInfo bufferCreateInfo = {VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
bufferCreateInfo.size = size; bufferCreateInfo.size = size;
bufferCreateInfo.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; bufferCreateInfo.usage = VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT;
VmaAllocationCreateInfo allocCreateInfo = {}; VmaAllocationCreateInfo allocCreateInfo = {};
allocCreateInfo.usage = VMA_MEMORY_USAGE_AUTO; allocCreateInfo.usage = VMA_MEMORY_USAGE_AUTO;
allocCreateInfo.flags = VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT; allocCreateInfo.flags = VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
vmaCreateBuffer(allocator, &bufferCreateInfo, &allocCreateInfo, &buffer, &alloc, nullptr); VkBuffer buf;
vmaCreateBuffer(allocator, &bufferCreateInfo, &allocCreateInfo, &buf, &alloc, nullptr);
buffer = Buffer(device, buf);
vmaCopyMemoryToAllocation(allocator, data, alloc, 0, size); VkBufferCreateInfo stagingBufInfo = {
.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO,
.size = size,
.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT,
};
VmaAllocationCreateInfo stagingAllocInfo = {
.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT,
.usage = VMA_MEMORY_USAGE_AUTO,
};
VkBuffer stagingBuf;
VmaAllocation stagingAllocation;
vmaCreateBuffer(allocator, &stagingBufInfo, &stagingAllocInfo, &stagingBuf, &stagingAllocation, nullptr);
Buffer stagingBuffer = Buffer(device, stagingBuf);
vmaCopyMemoryToAllocation(allocator, data, stagingAllocation, 0, size);
CommandBuffer copyCmd =
std::move(device.allocateCommandBuffers(vk::CommandBufferAllocateInfo(cmdPool, vk::CommandBufferLevel::ePrimary, 1)).front());
copyCmd.begin(vk::CommandBufferBeginInfo(vk::CommandBufferUsageFlagBits::eOneTimeSubmit));
copyCmd.copyBuffer(stagingBuffer, buffer, vk::BufferCopy(0, 0, size));
copyCmd.end();
queue.submit(vk::SubmitInfo({}, {}, *copyCmd, {}));
device.waitIdle();
} }
+19 -16
View File
@@ -9,12 +9,12 @@ using namespace vk::raii;
class GPUScene : public Scene class GPUScene : public Scene
{ {
public: public:
GPUScene(Device& device, VmaAllocator& allocator, CommandPool& cmdPool); GPUScene(Device& device, VmaAllocator& allocator, CommandPool& cmdPool, Queue& queue);
virtual ~GPUScene(); virtual ~GPUScene();
virtual void generate() override; virtual void createRayTracingHierarchy() override;
private: private:
void createStorageBuffer(VkBuffer& buffer, VmaAllocation& alloc, void* data, size_t size); void createStorageBuffer(Buffer& buffer, VmaAllocation& alloc, void* data, size_t size);
Device& device; Device& device;
VmaAllocator& allocator; VmaAllocator& allocator;
@@ -24,38 +24,41 @@ private:
// bottom level acceleration structure // bottom level acceleration structure
struct BLAS struct BLAS
{ {
vk::AccelerationStructureKHR handle; vk::AccelerationStructureKHR handle = nullptr;
VkBuffer buffer; vk::Buffer buffer = nullptr;
VmaAllocation alloc; VmaAllocation alloc = nullptr;
}; };
AccelerationStructureKHR accelerationStructure; AccelerationStructureKHR accelerationStructure = nullptr;
VmaAllocation accelerationAllocation; Buffer accelerationBuffer = nullptr;
VmaAllocation accelerationAllocation = nullptr;
Buffer instanceBuffer = nullptr;
VmaAllocation instanceAllocation = nullptr;
std::vector<BLAS> blas; std::vector<BLAS> blas;
VkBuffer modelBuffer; Buffer modelBuffer = nullptr;
VmaAllocation modelAllocation; VmaAllocation modelAllocation;
VkBuffer materialBuffer; Buffer materialBuffer = nullptr;
VmaAllocation materialAllocation; VmaAllocation materialAllocation;
VkBuffer positionBuffer; Buffer positionBuffer = nullptr;
VmaAllocation positionAllocation; VmaAllocation positionAllocation;
VkBuffer texCoordsBuffer; Buffer texCoordsBuffer = nullptr;
VmaAllocation texCoordsAllocation; VmaAllocation texCoordsAllocation;
VkBuffer normalsBuffer; Buffer normalsBuffer = nullptr;
VmaAllocation normalsAllocation; VmaAllocation normalsAllocation;
VkBuffer directionalLightBuffer; Buffer directionalLightBuffer = nullptr;
VmaAllocation directionalLightAllocation; VmaAllocation directionalLightAllocation;
VkBuffer pointLightBuffer; Buffer pointLightBuffer = nullptr;
VmaAllocation pointLightAllocation; VmaAllocation pointLightAllocation;
VkBuffer indexBuffer; Buffer indexBuffer = nullptr;
VmaAllocation indexAllocation; VmaAllocation indexAllocation;
friend class GPURenderer; friend class GPURenderer;
}; };
+12 -11
View File
@@ -1,3 +1,4 @@
#include "gpu/GPURenderer.h"
#include "scene/Renderer.h" #include "scene/Renderer.h"
#include "util/ModelLoader.h" #include "util/ModelLoader.h"
#include "window/Window.h" #include "window/Window.h"
@@ -6,19 +7,19 @@
int main() int main()
{ {
Renderer scene; std::unique_ptr<Renderer> scene = std::make_unique<Renderer>();
Window window(1920, 1080); Window window(1920, 1080);
Camera camera = Camera{ Camera camera = Camera{
.position = glm::vec3(-30, 5, 5), .position = glm::vec3(5, 1, 2),
.target = glm::vec3(0, 0, 0), .target = glm::vec3(0, 0, 0),
.S_O = 40, .S_O = 6,
}; };
RenderParameter render = RenderParameter{ RenderParameter render = RenderParameter{
.width = 1920, .width = 1920,
.height = 1080, .height = 1080,
.numSamples = 10000, .numSamples = 10000,
}; };
scene.startRender(camera, render); scene->startRender(camera, render);
while (true) while (true)
{ {
@@ -30,17 +31,17 @@ int main()
ImGui::InputFloat("Aperture", &camera.A); ImGui::InputFloat("Aperture", &camera.A);
ImGui::InputFloat("S_O", &camera.S_O); ImGui::InputFloat("S_O", &camera.S_O);
ImGui::Text("Render Parameters"); ImGui::Text("Render Parameters");
ImGui::InputInt2("Dimensions", &render.width); ImGui::InputInt2("Dimensions", (int*)&render.width);
ImGui::InputInt("Samples", &render.numSamples); ImGui::InputInt("Samples", (int*)&render.numSamples);
if (ImGui::Button("Render")) if (ImGui::Button("Render"))
{ {
scene.startRender(camera, render); scene->startRender(camera, render);
} }
ImGui::Text("Render Stats"); ImGui::Text("Render Stats");
ImGui::Text("Last Sample Time: %.3f ms", scene.getLastSampleTime()); ImGui::Text("Last Sample Time: %.3f ms", scene->getLastSampleTime());
ImGui::Text("Average Sample Time: %.3f ms", scene.getAverageSampleTime()); ImGui::Text("Average Sample Time: %.3f ms", scene->getAverageSampleTime());
ImGui::PlotLines("Sample Times", scene.getSampleTimes().data(), scene.getSampleTimes().size(), 0, 0, FLT_MAX, FLT_MAX, ImVec2(0, 40)); ImGui::PlotLines("Sample Times", scene->getSampleTimes().data(), scene->getSampleTimes().size(), 0, 0, FLT_MAX, FLT_MAX, ImVec2(0, 40));
window.update(scene.getImage()); window.update(scene->getImage());
} }
return 0; return 0;
} }
+7 -4
View File
@@ -1,4 +1,5 @@
#include "Renderer.h" #include "Renderer.h"
#include "gpu/GPUScene.h"
#include "util/ModelLoader.h" #include "util/ModelLoader.h"
#include <chrono> #include <chrono>
#include <iostream> #include <iostream>
@@ -6,14 +7,16 @@
Renderer::Renderer() Renderer::Renderer()
{ {
bvh.addDirectionalLight(DirectionalLight{ scene = std::make_unique<Scene>();
scene->addDirectionalLight(DirectionalLight{
.direction = glm::normalize(glm::vec3(-0.4f, -0.3f, -0.2f)), .direction = glm::normalize(glm::vec3(-0.4f, -0.3f, -0.2f)),
.color = glm::vec3(1, 1, 1), .color = glm::vec3(1, 1, 1),
}); });
bvh.addModels(ModelLoader::loadModel("../res/models/stanford-bunny.obj"), scene->addModels(ModelLoader::loadModel("../res/models/cube.fbx"),
glm::mat4(glm::vec4(1.0f, 0.0f, 0.0f, 0.0f), glm::vec4(0.0f, 1.0f, 0.0f, 0.0f), glm::vec4(0.0f, 0.0f, 1.0f, 0.0f), glm::mat4(glm::vec4(1.0f, 0.0f, 0.0f, 0.0f), glm::vec4(0.0f, 1.0f, 0.0f, 0.0f), glm::vec4(0.0f, 0.0f, 1.0f, 0.0f),
glm::vec4(0.0f, 0.0f, 0.0f, 1.0f))); glm::vec4(0.0f, 0.0f, 0.0f, 1.0f)));
bvh.generate(); scene->generate();
} }
Renderer::~Renderer() {} Renderer::~Renderer() {}
@@ -94,7 +97,7 @@ void Renderer::render(Camera camera, RenderParameter params)
glm::vec3 focus = r.origin + t * r.direction; glm::vec3 focus = r.origin + t * r.direction;
// r = Ray(lensSample, normalize(focus - lensSample)); // TODO: Fix lens // r = Ray(lensSample, normalize(focus - lensSample)); // TODO: Fix lens
bvh.traceRay(r, payload, 1e-4, 1e20); scene->traceRay(r, payload, 1e-4, 1e20);
accumulator[w + h * params.width] += payload.accumulatedRadiance / float(params.numSamples); accumulator[w + h * params.width] += payload.accumulatedRadiance / float(params.numSamples);
} }
+5 -5
View File
@@ -7,9 +7,9 @@
struct RenderParameter struct RenderParameter
{ {
int width; uint32_t width;
int height; uint32_t height;
int numSamples; uint32_t numSamples;
}; };
class Renderer class Renderer
@@ -26,7 +26,7 @@ public:
return std::accumulate(sampleTimes.begin(), sampleTimes.end(), 0.0f) / sampleTimes.size(); return std::accumulate(sampleTimes.begin(), sampleTimes.end(), 0.0f) / sampleTimes.size();
} }
private: protected:
virtual void render(Camera cam, RenderParameter params); virtual void render(Camera cam, RenderParameter params);
ThreadPool threadPool; ThreadPool threadPool;
std::thread worker; std::thread worker;
@@ -40,5 +40,5 @@ private:
std::vector<glm::vec3> accumulator; std::vector<glm::vec3> accumulator;
std::vector<PointLight> pointLights; std::vector<PointLight> pointLights;
std::vector<DirectionalLight> directionalLights; std::vector<DirectionalLight> directionalLights;
Scene bvh; std::unique_ptr<Scene> scene;
}; };
+53 -52
View File
@@ -20,42 +20,32 @@ void Scene::addModels(std::vector<PModel> _models, glm::mat4 transform)
void Scene::generate() void Scene::generate()
{ {
std::vector<PNode> pendingNodes; // todo: clear everything
for (const auto& [model, ref] : std::views::zip(models, refs)) for (uint32_t i = 0; i < models.size(); ++i)
{ {
pendingNodes.push_back(std::make_unique<Node>(model->boundingBox, ref)); auto& model = models[i];
} ModelReference ref = {
while (pendingNodes.size() > 1) .positionOffset = (uint32_t)positionPool.size(),
.numPositions = (uint32_t)model->positions.size(),
.indicesOffset = (uint32_t)indicesPool.size(),
.numIndices = (uint32_t)model->indices.size(),
};
for (uint32_t i = 0; i < model->positions.size(); ++i)
{ {
int lhs = pendingNodes.size(); positionPool.push_back(model->positions[i]);
int rhs = pendingNodes.size(); texCoordsPool.push_back(model->texCoords[i]);
float minSurface = std::numeric_limits<float>::max(); normalsPool.push_back(model->normals[i]);
for (int i = 0; i < pendingNodes.size(); ++i) }
for (uint32_t i = 0; i < model->indices.size(); ++i)
{ {
for (int j = 0; j < pendingNodes.size(); ++j) indicesPool.push_back(model->indices[i]);
{ edgesPool.push_back(model->edges[i * 2 + 0]);
if (i == j) edgesPool.push_back(model->edges[i * 2 + 1]);
continue; faceNormalsPool.push_back(glm::normalize(model->faceNormals[i]));
AABB combined = AABB::combine(pendingNodes[i]->aabb, pendingNodes[j]->aabb);
float surface = combined.surfaceArea();
if (minSurface > surface)
{
lhs = i;
rhs = j;
minSurface = surface;
} }
refs.push_back(ref);
} }
} createRayTracingHierarchy();
PNode newNode = std::make_unique<Node>(AABB::combine(pendingNodes[lhs]->aabb, pendingNodes[rhs]->aabb));
newNode->left = std::move(pendingNodes[lhs]);
newNode->right = std::move(pendingNodes[rhs]);
assert(rhs > lhs);
//
pendingNodes.erase(pendingNodes.begin() + rhs);
pendingNodes.erase(pendingNodes.begin() + lhs);
pendingNodes.push_back(std::move(newNode));
}
hierarchy = std::move(pendingNodes[0]);
} }
void Scene::traceRay(Ray ray, Payload& payload, const float tmin, const float tmax) const noexcept void Scene::traceRay(Ray ray, Payload& payload, const float tmin, const float tmax) const noexcept
@@ -119,34 +109,45 @@ void Scene::traceRay(Ray ray, Payload& payload, const float tmin, const float tm
} }
} }
void Scene::populateGeometryPools() void Scene::createRayTracingHierarchy()
{ {
//todo: clear everything std::vector<PNode> pendingNodes;
for(uint32_t i = 0; i < models.size(); ++i) for (const auto& [model, ref] : std::views::zip(models, refs))
{ {
auto& model = models[i]; pendingNodes.push_back(std::make_unique<Node>(model->boundingBox, ref));
ModelReference ref = {
.positionOffset = (uint32_t)positionPool.size(),
.numPositions = (uint32_t)model->positions.size(),
.indicesOffset = (uint32_t)indicesPool.size(),
.numIndices = (uint32_t)model->indices.size(),
};
for (uint32_t i = 0; i < model->positions.size(); ++i)
{
positionPool.push_back(model->positions[i]);
texCoordsPool.push_back(model->texCoords[i]);
normalsPool.push_back(model->normals[i]);
} }
for (uint32_t i = 0; i < model->indices.size(); ++i) while (pendingNodes.size() > 1)
{ {
indicesPool.push_back(model->indices[i]); int lhs = pendingNodes.size();
edgesPool.push_back(model->edges[i * 2 + 0]); int rhs = pendingNodes.size();
edgesPool.push_back(model->edges[i * 2 + 1]); float minSurface = std::numeric_limits<float>::max();
faceNormalsPool.push_back(glm::normalize(model->faceNormals[i])); for (int i = 0; i < pendingNodes.size(); ++i)
{
for (int j = 0; j < pendingNodes.size(); ++j)
{
if (i == j)
continue;
AABB combined = AABB::combine(pendingNodes[i]->aabb, pendingNodes[j]->aabb);
float surface = combined.surfaceArea();
if (minSurface > surface)
{
lhs = i;
rhs = j;
minSurface = surface;
} }
refs.push_back(ref);
} }
} }
PNode newNode = std::make_unique<Node>(AABB::combine(pendingNodes[lhs]->aabb, pendingNodes[rhs]->aabb));
newNode->left = std::move(pendingNodes[lhs]);
newNode->right = std::move(pendingNodes[rhs]);
assert(rhs > lhs);
//
pendingNodes.erase(pendingNodes.begin() + rhs);
pendingNodes.erase(pendingNodes.begin() + lhs);
pendingNodes.push_back(std::move(newNode));
}
hierarchy = std::move(pendingNodes[0]);
}
bool Scene::testIntersection(const PNode& currentNode, const Ray ray, const float tmin, float tmax) const noexcept bool Scene::testIntersection(const PNode& currentNode, const Ray ray, const float tmin, float tmax) const noexcept
{ {
+3 -2
View File
@@ -37,7 +37,7 @@ public:
void addDirectionalLight(DirectionalLight dir) { directionalLights.push_back(dir); } void addDirectionalLight(DirectionalLight dir) { directionalLights.push_back(dir); }
void addModel(PModel model, glm::mat4 transform); void addModel(PModel model, glm::mat4 transform);
void addModels(std::vector<PModel> models, glm::mat4 transform); void addModels(std::vector<PModel> models, glm::mat4 transform);
virtual void generate(); void generate();
void traceRay(Ray ray, Payload& payload, const float tmin, const float tmax) const noexcept; void traceRay(Ray ray, Payload& payload, const float tmin, const float tmax) const noexcept;
@@ -66,11 +66,12 @@ protected:
PNode hierarchy; PNode hierarchy;
std::vector<PModel> models; std::vector<PModel> models;
void populateGeometryPools(); virtual void createRayTracingHierarchy();
// tests if a ray intersects any geometry, no hit information, for shadow rays // tests if a ray intersects any geometry, no hit information, for shadow rays
bool testIntersection(const PNode& currentNode, const Ray ray, const float tmin, const float tmax) const noexcept; bool testIntersection(const PNode& currentNode, const Ray ray, const float tmin, const float tmax) const noexcept;
IntersectionInfo generateIntersections(const PNode& currentNode, const Ray ray, const float tmin, const float tmax) const noexcept; IntersectionInfo generateIntersections(const PNode& currentNode, const Ray ray, const float tmin, const float tmax) const noexcept;
bool testModel(const ModelReference& reference, const Ray ray, const float tmin, const float tmax) const noexcept; bool testModel(const ModelReference& reference, const Ray ray, const float tmin, const float tmax) const noexcept;
IntersectionInfo intersectModel(const ModelReference& reference, const Ray ray, const float tmin, const float tmax) const noexcept; IntersectionInfo intersectModel(const ModelReference& reference, const Ray ray, const float tmin, const float tmax) const noexcept;
friend class GPURenderer;
}; };
+2 -1
View File
@@ -8,7 +8,7 @@
std::vector<PModel> ModelLoader::loadModel(std::string_view filename) std::vector<PModel> ModelLoader::loadModel(std::string_view filename)
{ {
Assimp::Importer importer; Assimp::Importer importer;
const aiScene* scene = importer.ReadFile(std::string(filename), aiProcess_Triangulate); const aiScene* scene = importer.ReadFile(std::string(filename), aiProcess_Triangulate | aiProcess_GenNormals);
std::cout << importer.GetErrorString() << std::endl; std::cout << importer.GetErrorString() << std::endl;
std::vector<PModel> result; std::vector<PModel> result;
for (int m = 0; m < scene->mNumMeshes; ++m) for (int m = 0; m < scene->mNumMeshes; ++m)
@@ -28,6 +28,7 @@ std::vector<PModel> ModelLoader::loadModel(std::string_view filename)
{ {
model->texCoords.push_back(glm::vec2(0, 0)); model->texCoords.push_back(glm::vec2(0, 0));
} }
model->normals.push_back(glm::vec3(mesh->mNormals[v].x, mesh->mNormals[v].y, mesh->mNormals[v].z));
aabb.adjust(model->positions.back()); aabb.adjust(model->positions.back());
} }
for (int i = 0; i < mesh->mNumFaces; ++i) for (int i = 0; i < mesh->mNumFaces; ++i)