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

@ -60,7 +60,8 @@ source_group("Shader Headers" FILES ${GLSL_HEADERS})
#-------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------
# Linkage # Linkage
# #
target_link_libraries(${PROJNAME} ${PLATFORM_LIBRARIES} nvpro_core) find_package(UTK PATHS ../../utk_experiments/utk)
target_link_libraries(${PROJNAME} ${PLATFORM_LIBRARIES} nvpro_core UTK_LIBRARY)
foreach(DEBUGLIB ${LIBRARIES_DEBUG}) foreach(DEBUGLIB ${LIBRARIES_DEBUG})
target_link_libraries(${PROJNAME} debug ${DEBUGLIB}) target_link_libraries(${PROJNAME} debug ${DEBUGLIB})

View file

@ -20,6 +20,8 @@
#include <sstream> #include <sstream>
#include <iostream> #include <iostream>
#include <utk/samplers/SamplerStep_Custom.hpp>
#include <utk/utils/PointsetIO.hpp>
#define TINYGLTF_IMPLEMENTATION #define TINYGLTF_IMPLEMENTATION
@ -116,11 +118,11 @@ void HelloVulkan::createDescriptorSetLayout()
bind.addBinding(SceneBindings::eTextures, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, nbTextures, 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); VK_SHADER_STAGE_FRAGMENT_BIT | VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR | VK_SHADER_STAGE_ANY_HIT_BIT_KHR);
// Scene buffers // 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_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT | VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR
| VK_SHADER_STAGE_ANY_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_descSetLayout = m_descSetLayoutBind.createLayout(m_device);
m_descPool = m_descSetLayoutBind.createPool(m_device, 1); m_descPool = m_descSetLayoutBind.createPool(m_device, 1);
@ -137,17 +139,20 @@ void HelloVulkan::updateDescriptorSet()
// Camera matrices and scene description // Camera matrices and scene description
VkDescriptorBufferInfo dbiUnif{m_bGlobals.buffer, 0, VK_WHOLE_SIZE}; VkDescriptorBufferInfo dbiUnif{m_bGlobals.buffer, 0, VK_WHOLE_SIZE};
VkDescriptorBufferInfo sceneDesc{m_sceneDesc.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, 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 // All texture samplers
std::vector<VkDescriptorImageInfo> diit; std::vector<VkDescriptorImageInfo> diit;
for(auto& texture : m_textures) for(auto& texture : m_textures)
{
diit.emplace_back(texture.descriptor); diit.emplace_back(texture.descriptor);
}
writes.emplace_back(m_descSetLayoutBind.makeWriteArray(m_descSet, SceneBindings::eTextures, diit.data())); 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 // Writing the information
vkUpdateDescriptorSets(m_device, static_cast<uint32_t>(writes.size()), writes.data(), 0, nullptr); 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 // Creates all textures found
createTextureImages(cmdBuf, tmodel); createTextureImages(cmdBuf, tmodel);
//generate and submit pointset //generate and submit pointset
createPointsetTexture(cmdBuf); createPointsetImage(cmdBuf);
cmdBufGet.submitAndWait(cmdBuf); cmdBufGet.submitAndWait(cmdBuf);
m_alloc.finalizeAndReleaseStaging(); m_alloc.finalizeAndReleaseStaging();
@ -268,6 +273,7 @@ void HelloVulkan::loadScene(const std::string& filename)
NAME_VK(m_materialBuffer.buffer); NAME_VK(m_materialBuffer.buffer);
NAME_VK(m_primInfo.buffer); NAME_VK(m_primInfo.buffer);
NAME_VK(m_sceneDesc.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}; std::vector<vec2> points(m_size.height * m_size.width, vec2(0.f));
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;
auto swapchainExtent = this->m_swapChain.getExtent();
// TODO: generate and parse pointset here //generate blue noise points
std::vector<float_t> points{}; const int npoints = 256; //points.size() / NCHANNELS;
points.reserve(swapchainExtent.height * swapchainExtent.width * 2 * 4); 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); 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)};
float rand_y = static_cast<float>(rand()) / static_cast<float_t>(RAND_MAX);
points.push_back(rand_x);
points.push_back(rand_y);
} }
} }
VkImageCreateInfo imageCreateInfo = m_pointsetBuffer = m_alloc.createBuffer(cmdBuf, sizeof(glm::vec2) * points.size(), points.data(),
nvvk::makeImage2DCreateInfo(swapchainExtent, VK_FORMAT_R32G32_SFLOAT, VK_IMAGE_USAGE_SAMPLED_BIT, false); VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT);
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");
} }
@ -393,13 +401,13 @@ void HelloVulkan::destroyResources()
m_alloc.destroy(m_materialBuffer); m_alloc.destroy(m_materialBuffer);
m_alloc.destroy(m_primInfo); m_alloc.destroy(m_primInfo);
m_alloc.destroy(m_sceneDesc); m_alloc.destroy(m_sceneDesc);
m_alloc.destroy(m_pointsetBuffer);
for(auto& t : m_textures) for(auto& t : m_textures)
{ {
m_alloc.destroy(t); m_alloc.destroy(t);
} }
m_alloc.destroy(m_pointset);
//#Post //#Post
m_alloc.destroy(m_offscreenColor); m_alloc.destroy(m_offscreenColor);

View file

@ -49,7 +49,7 @@ public:
void updateDescriptorSet(); void updateDescriptorSet();
void createUniformBuffer(); void createUniformBuffer();
void createTextureImages(const VkCommandBuffer& cmdBuf, tinygltf::Model& gltfModel); void createTextureImages(const VkCommandBuffer& cmdBuf, tinygltf::Model& gltfModel);
void createPointsetTexture(const VkCommandBuffer& cmdBuf); void createPointsetImage(const VkCommandBuffer& cmdBuf);
void updateUniformBuffer(const VkCommandBuffer& cmdBuf); void updateUniformBuffer(const VkCommandBuffer& cmdBuf);
void onResize(int /*w*/, int /*h*/) override; void onResize(int /*w*/, int /*h*/) override;
void destroyResources(); void destroyResources();
@ -64,6 +64,7 @@ public:
nvvk::Buffer m_materialBuffer; nvvk::Buffer m_materialBuffer;
nvvk::Buffer m_primInfo; nvvk::Buffer m_primInfo;
nvvk::Buffer m_sceneDesc; nvvk::Buffer m_sceneDesc;
nvvk::Buffer m_pointsetBuffer;
// Information pushed at each draw call // Information pushed at each draw call
PushConstantRaster m_pcRaster{ PushConstantRaster m_pcRaster{
@ -85,7 +86,6 @@ public:
nvvk::Buffer m_bGlobals; // Device-Host of the camera matrices nvvk::Buffer m_bGlobals; // Device-Host of the camera matrices
std::vector<nvvk::Texture> m_textures; // vector of all textures of the scene std::vector<nvvk::Texture> m_textures; // vector of all textures of the scene
nvvk::Texture m_pointset; // Texture for starting locations of primary rays
nvvk::ResourceAllocatorDma m_alloc; // Allocator for buffer, images, acceleration structures nvvk::ResourceAllocatorDma m_alloc; // Allocator for buffer, images, acceleration structures
nvvk::DebugUtil m_debug; // Utility to name objects nvvk::DebugUtil m_debug; // Utility to name objects

View file

@ -31,6 +31,7 @@
#include "imgui/imgui_helper.h" #include "imgui/imgui_helper.h"
#include "hello_vulkan.h" #include "hello_vulkan.h"
#include "shaders/host_device.h"
#include "imgui/imgui_camera_widget.h" #include "imgui/imgui_camera_widget.h"
#include "nvh/cameramanipulator.hpp" #include "nvh/cameramanipulator.hpp"
#include "nvh/fileoperations.hpp" #include "nvh/fileoperations.hpp"
@ -71,8 +72,8 @@ void renderUI(HelloVulkan& helloVk, bool useRaytracer)
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
static int const SAMPLE_WIDTH = 800; static int const SAMPLE_WIDTH = WINDOWSIZE;
static int const SAMPLE_HEIGHT = 800; static int const SAMPLE_HEIGHT = WINDOWSIZE;
//-------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------

View file

@ -44,14 +44,14 @@ using uint = unsigned int;
START_BINDING(SceneBindings) START_BINDING(SceneBindings)
eGlobals = 0, // Global uniform containing camera matrices eGlobals = 0, // Global uniform containing camera matrices
eSceneDesc = 1, // Access to the scene buffers eSceneDesc = 1, // Access to the scene buffers
eTextures = 2, // Access to textures eTextures = 2, // Access to textures
ePointsetTexture = 3 //Texture containing the pointset for primary raygen ePointset = 3 // Pointset
END_BINDING(); END_BINDING();
START_BINDING(RtxBindings) START_BINDING(RtxBindings)
eTlas = 0, // Top-level acceleration structure eTlas = 0, // Top-level acceleration structure
eOutImage = 1, // Ray tracer output image eOutImage = 1, // Ray tracer output image
ePrimLookup = 2 // Lookup of objects ePrimLookup = 2 // Lookup of objects
END_BINDING(); END_BINDING();
// clang-format on // clang-format on
@ -111,4 +111,6 @@ struct GltfShadeMaterial
int pbrBaseColorTexture; int pbrBaseColorTexture;
}; };
#define WINDOWSIZE 600
#endif #endif

View file

@ -33,16 +33,14 @@ layout(set = 0, binding = 0) uniform accelerationStructureEXT topLevelAS;
layout(set = 0, binding = 1, rgba32f) uniform image2D image; layout(set = 0, binding = 1, rgba32f) uniform image2D image;
layout(set = 1, binding = 0) uniform _GlobalUniforms { GlobalUniforms uni; }; layout(set = 1, binding = 0) uniform _GlobalUniforms { GlobalUniforms uni; };
layout(set = 1, binding = ePointset) buffer PointsetBuffer { vec2[WINDOWSIZE * WINDOWSIZE] pointset; };
layout(push_constant) uniform _PushConstantRay { PushConstantRay pcRay; }; layout(push_constant) uniform _PushConstantRay { PushConstantRay pcRay; };
layout(set = 1, binding = ePointsetTexture) uniform sampler2D pointset;// bluenoise pointset
// clang-format on // clang-format on
void main() void main()
{ {
vec2 uv = vec2(float(gl_LaunchIDEXT.x) / float(gl_LaunchSizeEXT.x), float(gl_LaunchIDEXT.y) / float(gl_LaunchSizeEXT.y)); /*
const vec2 rayOriginInPix = vec2(gl_LaunchIDEXT.xy) + pointset[255].xy;
const vec2 rayOriginInPix = vec2(gl_LaunchIDEXT.xy) + texture(pointset, uv).xy;
const vec2 inUV = rayOriginInPix / vec2(gl_LaunchSizeEXT.xy); const vec2 inUV = rayOriginInPix / vec2(gl_LaunchSizeEXT.xy);
vec2 d = inUV * 2.0 - 1.0; vec2 d = inUV * 2.0 - 1.0;
@ -66,6 +64,6 @@ void main()
tMax, // ray max range tMax, // ray max range
0// payload (location = 0) 0// payload (location = 0)
); );
*/
imageStore(image, ivec2(gl_LaunchIDEXT.xy), vec4(prd.hitValue, 1.0)); imageStore(image, ivec2(gl_LaunchIDEXT.xy), vec4(pointset[gl_LaunchIDEXT.x + WINDOWSIZE * gl_LaunchIDEXT.y].xy, 0.f, 1.0));
} }