diff --git a/raytracer/ray_tracing_gltf/CMakeLists.txt b/raytracer/ray_tracing_gltf/CMakeLists.txt index 1bf130c..9936012 100644 --- a/raytracer/ray_tracing_gltf/CMakeLists.txt +++ b/raytracer/ray_tracing_gltf/CMakeLists.txt @@ -60,7 +60,8 @@ source_group("Shader Headers" FILES ${GLSL_HEADERS}) #-------------------------------------------------------------------------------------------------- # 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}) target_link_libraries(${PROJNAME} debug ${DEBUGLIB}) diff --git a/raytracer/ray_tracing_gltf/hello_vulkan.cpp b/raytracer/ray_tracing_gltf/hello_vulkan.cpp index f8f02b3..8cb79c9 100644 --- a/raytracer/ray_tracing_gltf/hello_vulkan.cpp +++ b/raytracer/ray_tracing_gltf/hello_vulkan.cpp @@ -20,6 +20,8 @@ #include #include +#include +#include #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 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(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 points(m_size.height * m_size.width, vec2(0.f)); - auto swapchainExtent = this->m_swapChain.getExtent(); - // TODO: generate and parse pointset here - std::vector 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{}; + 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(rand()) / static_cast(RAND_MAX); - float rand_y = static_cast(rand()) / static_cast(RAND_MAX); - points.push_back(rand_x); - points.push_back(rand_y); + points[x + y * m_size.width] = vec2{static_cast(x) / static_cast(m_size.width), static_cast(y) / static_cast(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); diff --git a/raytracer/ray_tracing_gltf/hello_vulkan.h b/raytracer/ray_tracing_gltf/hello_vulkan.h index 97339fa..3288655 100644 --- a/raytracer/ray_tracing_gltf/hello_vulkan.h +++ b/raytracer/ray_tracing_gltf/hello_vulkan.h @@ -49,7 +49,7 @@ public: void updateDescriptorSet(); void createUniformBuffer(); void createTextureImages(const VkCommandBuffer& cmdBuf, tinygltf::Model& gltfModel); - void createPointsetTexture(const VkCommandBuffer& cmdBuf); + void createPointsetImage(const VkCommandBuffer& cmdBuf); void updateUniformBuffer(const VkCommandBuffer& cmdBuf); void onResize(int /*w*/, int /*h*/) override; void destroyResources(); @@ -64,6 +64,7 @@ public: nvvk::Buffer m_materialBuffer; nvvk::Buffer m_primInfo; nvvk::Buffer m_sceneDesc; + nvvk::Buffer m_pointsetBuffer; // Information pushed at each draw call PushConstantRaster m_pcRaster{ @@ -85,7 +86,6 @@ public: nvvk::Buffer m_bGlobals; // Device-Host of the camera matrices std::vector 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::DebugUtil m_debug; // Utility to name objects diff --git a/raytracer/ray_tracing_gltf/main.cpp b/raytracer/ray_tracing_gltf/main.cpp index 1439e95..92f7b82 100644 --- a/raytracer/ray_tracing_gltf/main.cpp +++ b/raytracer/ray_tracing_gltf/main.cpp @@ -31,6 +31,7 @@ #include "imgui/imgui_helper.h" #include "hello_vulkan.h" +#include "shaders/host_device.h" #include "imgui/imgui_camera_widget.h" #include "nvh/cameramanipulator.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_HEIGHT = 800; +static int const SAMPLE_WIDTH = WINDOWSIZE; +static int const SAMPLE_HEIGHT = WINDOWSIZE; //-------------------------------------------------------------------------------------------------- diff --git a/raytracer/ray_tracing_gltf/shaders/host_device.h b/raytracer/ray_tracing_gltf/shaders/host_device.h index bb5797e..64104ea 100644 --- a/raytracer/ray_tracing_gltf/shaders/host_device.h +++ b/raytracer/ray_tracing_gltf/shaders/host_device.h @@ -44,14 +44,14 @@ using uint = unsigned int; START_BINDING(SceneBindings) eGlobals = 0, // Global uniform containing camera matrices eSceneDesc = 1, // Access to the scene buffers - eTextures = 2, // Access to textures - ePointsetTexture = 3 //Texture containing the pointset for primary raygen + eTextures = 2, // Access to textures + ePointset = 3 // Pointset END_BINDING(); START_BINDING(RtxBindings) - eTlas = 0, // Top-level acceleration structure - eOutImage = 1, // Ray tracer output image - ePrimLookup = 2 // Lookup of objects + eTlas = 0, // Top-level acceleration structure + eOutImage = 1, // Ray tracer output image + ePrimLookup = 2 // Lookup of objects END_BINDING(); // clang-format on @@ -111,4 +111,6 @@ struct GltfShadeMaterial int pbrBaseColorTexture; }; +#define WINDOWSIZE 600 + #endif diff --git a/raytracer/ray_tracing_gltf/shaders/raytrace.rgen b/raytracer/ray_tracing_gltf/shaders/raytrace.rgen index 0af3b42..535530d 100644 --- a/raytracer/ray_tracing_gltf/shaders/raytrace.rgen +++ b/raytracer/ray_tracing_gltf/shaders/raytrace.rgen @@ -33,16 +33,14 @@ layout(set = 0, binding = 0) uniform accelerationStructureEXT topLevelAS; layout(set = 0, binding = 1, rgba32f) uniform image2D image; 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(set = 1, binding = ePointsetTexture) uniform sampler2D pointset;// bluenoise pointset // clang-format on 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) + texture(pointset, uv).xy; + /* + const vec2 rayOriginInPix = vec2(gl_LaunchIDEXT.xy) + pointset[255].xy; const vec2 inUV = rayOriginInPix / vec2(gl_LaunchSizeEXT.xy); vec2 d = inUV * 2.0 - 1.0; @@ -66,6 +64,6 @@ void main() tMax, // ray max range 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)); }