moving pointsets to the GPU via Uniform buffer

This commit is contained in:
CDaut 2024-07-09 20:18:28 +02:00
parent 74457d1a9c
commit 1114f4c8ff
Signed by: clara
GPG key ID: 223391B52FAD4463
6 changed files with 61 additions and 51 deletions

View file

@ -20,6 +20,8 @@
#include <sstream>
#include <iostream>
#include <utk/samplers/SamplerStep_Custom.hpp>
#include <utk/utils/PointsetIO.hpp>
#define TINYGLTF_IMPLEMENTATION
@ -116,11 +118,11 @@ void HelloVulkan::createDescriptorSetLayout()
bind.addBinding(SceneBindings::eTextures, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, nbTextures,
VK_SHADER_STAGE_FRAGMENT_BIT | VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR | VK_SHADER_STAGE_ANY_HIT_BIT_KHR);
// Scene buffers
bind.addBinding(eSceneDesc, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
bind.addBinding(SceneBindings::eSceneDesc, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT | VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR
| VK_SHADER_STAGE_ANY_HIT_BIT_KHR);
//Primary Raygen texture
bind.addBinding(SceneBindings::ePointsetTexture, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1, VK_SHADER_STAGE_RAYGEN_BIT_KHR);
bind.addBinding(SceneBindings::ePointset, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1, VK_SHADER_STAGE_RAYGEN_BIT_KHR);
m_descSetLayout = m_descSetLayoutBind.createLayout(m_device);
m_descPool = m_descSetLayoutBind.createPool(m_device, 1);
@ -137,17 +139,20 @@ void HelloVulkan::updateDescriptorSet()
// Camera matrices and scene description
VkDescriptorBufferInfo dbiUnif{m_bGlobals.buffer, 0, VK_WHOLE_SIZE};
VkDescriptorBufferInfo sceneDesc{m_sceneDesc.buffer, 0, VK_WHOLE_SIZE};
VkDescriptorBufferInfo pointsetDesc{m_pointsetBuffer.buffer, 0, VK_WHOLE_SIZE};
writes.emplace_back(m_descSetLayoutBind.makeWrite(m_descSet, SceneBindings::eGlobals, &dbiUnif));
writes.emplace_back(m_descSetLayoutBind.makeWrite(m_descSet, eSceneDesc, &sceneDesc));
writes.emplace_back(m_descSetLayoutBind.makeWrite(m_descSet, SceneBindings::eSceneDesc, &sceneDesc));
writes.emplace_back(m_descSetLayoutBind.makeWrite(m_descSet, SceneBindings::ePointset, &pointsetDesc));
// All texture samplers
std::vector<VkDescriptorImageInfo> diit;
for(auto& texture : m_textures)
{
diit.emplace_back(texture.descriptor);
}
writes.emplace_back(m_descSetLayoutBind.makeWriteArray(m_descSet, SceneBindings::eTextures, diit.data()));
//add the pointset texture
writes.emplace_back(m_descSetLayoutBind.makeWrite(m_descSet, SceneBindings::ePointsetTexture, &m_pointset.descriptor));
// Writing the information
vkUpdateDescriptorSets(m_device, static_cast<uint32_t>(writes.size()), writes.data(), 0, nullptr);
@ -256,7 +261,7 @@ void HelloVulkan::loadScene(const std::string& filename)
// Creates all textures found
createTextureImages(cmdBuf, tmodel);
//generate and submit pointset
createPointsetTexture(cmdBuf);
createPointsetImage(cmdBuf);
cmdBufGet.submitAndWait(cmdBuf);
m_alloc.finalizeAndReleaseStaging();
@ -268,6 +273,7 @@ void HelloVulkan::loadScene(const std::string& filename)
NAME_VK(m_materialBuffer.buffer);
NAME_VK(m_primInfo.buffer);
NAME_VK(m_sceneDesc.buffer);
NAME_VK(m_pointsetBuffer.buffer);
}
@ -336,41 +342,43 @@ void HelloVulkan::createTextureImages(const VkCommandBuffer& cmdBuf, tinygltf::M
}
}
void HelloVulkan::createPointsetTexture(const VkCommandBuffer& cmdBuf)
void HelloVulkan::createPointsetImage(const VkCommandBuffer& cmdBuf)
{
VkSamplerCreateInfo samplerCreateInfo{VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO};
samplerCreateInfo.minFilter = VK_FILTER_NEAREST;
samplerCreateInfo.magFilter = VK_FILTER_NEAREST;
samplerCreateInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
samplerCreateInfo.maxLod = FLT_MAX;
samplerCreateInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerCreateInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerCreateInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
std::vector<vec2> points(m_size.height * m_size.width, vec2(0.f));
auto swapchainExtent = this->m_swapChain.getExtent();
// TODO: generate and parse pointset here
std::vector<float_t> points{};
points.reserve(swapchainExtent.height * swapchainExtent.width * 2 * 4);
//generate blue noise points
const int npoints = 256; //points.size() / NCHANNELS;
utk::CustomHeckSampler sampler(0.606f, 8.f, 0.5);
auto pointset = utk::Pointset<float>{};
std::cout << std::endl << "generating " << npoints << " blue noise points…" << std::endl;
sampler.generateSamples(pointset, npoints);
for(int y = 0; y < swapchainExtent.height * 4; ++y)
utk::write_text_pointset("biig_pointset.txt", pointset);
/*
//iterate pointset and fill texture
for(int i = 0; i < pointset.Npts(); ++i)
{
for(int x = 0; x < swapchainExtent.width; ++x)
auto point = std::make_pair(pointset[i][0] * (m_size.width - 1), pointset[i][1] * ((m_size.height) - 1));
float_t wholex, wholey, fractionalx, fractionaly;
fractionalx = std::modf(point.first, &wholex);
fractionaly = std::modf(point.second, &wholey);
points[wholex + wholey * m_size.width] = vec2{fractionalx, fractionaly};
}
*/
for(int y = 0; y < m_size.height; ++y)
{
for(int x = 0; x < m_size.width; ++x)
{
float rand_x = static_cast<float>(rand()) / static_cast<float_t>(RAND_MAX);
float rand_y = static_cast<float>(rand()) / static_cast<float_t>(RAND_MAX);
points.push_back(rand_x);
points.push_back(rand_y);
points[x + y * m_size.width] = vec2{static_cast<float_t>(x) / static_cast<float_t>(m_size.width), static_cast<float_t>(y) / static_cast<float_t>(m_size.height)};
}
}
VkImageCreateInfo imageCreateInfo =
nvvk::makeImage2DCreateInfo(swapchainExtent, VK_FORMAT_R32G32_SFLOAT, VK_IMAGE_USAGE_SAMPLED_BIT, false);
nvvk::Image image = m_alloc.createImage(cmdBuf, points.size(), points.data(), imageCreateInfo);
VkImageViewCreateInfo ivInfo = nvvk::makeImageViewCreateInfo(image.image, imageCreateInfo);
m_pointset = m_alloc.createTexture(image, ivInfo, samplerCreateInfo);
m_debug.setObjectName(m_pointset.image, "pointset");
m_pointsetBuffer = m_alloc.createBuffer(cmdBuf, sizeof(glm::vec2) * points.size(), points.data(),
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT);
}
@ -393,13 +401,13 @@ void HelloVulkan::destroyResources()
m_alloc.destroy(m_materialBuffer);
m_alloc.destroy(m_primInfo);
m_alloc.destroy(m_sceneDesc);
m_alloc.destroy(m_pointsetBuffer);
for(auto& t : m_textures)
{
m_alloc.destroy(t);
}
m_alloc.destroy(m_pointset);
//#Post
m_alloc.destroy(m_offscreenColor);