From 65c165f40765cf6b030c8d926aa8d3badb60efc6 Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Tue, 28 Jan 2025 00:08:52 +0100 Subject: [PATCH] lot more code --- CMakeLists.txt | 2 + res/shaders/ClosestHit.slang | 125 +++++++++++++++++++++++++++++ res/shaders/Common.slang | 121 ++++++++++++++++++++++++++++ res/shaders/RayGen.slang | 57 ++++++++++++++ src/gpu/CMakeLists.txt | 5 +- src/gpu/GPURenderer.cpp | 147 ++++++++++++++++++++++++++++++++++- src/gpu/GPURenderer.h | 22 ++++++ src/gpu/GPUScene.cpp | 124 +++++++++++++++++++++++++++++ src/gpu/GPUScene.h | 60 ++++++++++++++ src/scene/Scene.cpp | 50 +++++++----- src/scene/Scene.h | 12 ++- src/util/Model.cpp | 5 ++ src/util/Model.h | 1 + vcpkg.json | 1 + 14 files changed, 705 insertions(+), 27 deletions(-) create mode 100644 res/shaders/ClosestHit.slang create mode 100644 res/shaders/Common.slang create mode 100644 src/gpu/GPUScene.cpp create mode 100644 src/gpu/GPUScene.h diff --git a/CMakeLists.txt b/CMakeLists.txt index a358b0a..48a255a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -12,6 +12,7 @@ set(CMAKE_TOOLCHAIN_FILE ${CMAKE_CURRENT_SOURCE_DIR}/external/vcpkg/scripts/buil project(RayTracer) find_package(Vulkan REQUIRED) +find_package(VulkanMemoryAllocator CONFIG REQUIRED) find_package(glew CONFIG REQUIRED) find_package(assimp CONFIG REQUIRED) find_package(glfw3 CONFIG REQUIRED) @@ -23,6 +24,7 @@ add_executable(RayTracer "") target_include_directories(RayTracer PUBLIC src/) target_link_libraries(RayTracer PUBLIC Vulkan::Vulkan) target_link_libraries(RayTracer PUBLIC Vulkan::Headers) +target_link_libraries(RayTracer PUBLIC GPUOpen::VulkanMemoryAllocator) target_link_libraries(RayTracer PUBLIC assimp::assimp) target_link_libraries(RayTracer PUBLIC glfw) target_link_libraries(RayTracer PUBLIC imgui::imgui) diff --git a/res/shaders/ClosestHit.slang b/res/shaders/ClosestHit.slang new file mode 100644 index 0000000..de15710 --- /dev/null +++ b/res/shaders/ClosestHit.slang @@ -0,0 +1,125 @@ +import Common; + + +[shader("closesthit")] +void closestHit(inout RayPayload hitValue, in BuiltInTriangleIntersectionAttributes attr) +{ + hitValue.hit = true; + // todo: replace with anyhit shader + if(hitValue.anyHit) + return; + const float3 barycentricCoords = float3(1.0f - attr.barycentrics.x - attr.barycentrics.y, attr.barycentrics.x, attr.barycentrics.y); + + ModelReference m = pParams.modelData[InstanceID()]; + + // offset into the index buffer + uint indexOffset = m.indicesOffset; + // added to indices to reference correct part of global mesh pool + uint vertexOffset = m.positionOffset; + + uint vertexIndex0 = vertexOffset + pParams.indexBuffer[indexOffset + 3 * PrimitiveIndex() + 0]; + uint vertexIndex1 = vertexOffset + pParams.indexBuffer[indexOffset + 3 * PrimitiveIndex() + 1]; + uint vertexIndex2 = vertexOffset + pParams.indexBuffer[indexOffset + 3 * PrimitiveIndex() + 2]; + + Vertex attr0 = loadVertex(vertexIndex0); + Vertex attr1 = loadVertex(vertexIndex1); + Vertex attr2 = loadVertex(vertexIndex2); + + Vertex vert = Vertex.interpolate(attr0, attr1, attr2, barycentricCoords); + + float3 normalLight = dot(vert.normal, WorldRayDirection()) < 0 ? vert.normal : -vert.normal; + + MaterialParameter mat; // TOOD: + + hitValue.depth++; + float3 localAccRad = float3(0); + float3 rnd = rand01(uint3(vertexIndex0, vertexIndex1, vertexIndex2)); + //float kt = ka + ks; + //float s = -log(rnd.z) / kt; + //float3 xs = r.o + s * r.d; + //if (s < t) { + // float p = kt * rnd.z; + // if (depth > 5) { + // if (rnd.z >= p) break; + // else accmat /= p; + // } + // float3 ldirect = nextEventEstimation(accmat, r.d, xs, -r.d, kt, true, rnd); + // accrad += (fogEmm + ks * ldirect) / kt; + // accmat *= ks / kt; + // rayDesc.Origin = xs; + // rayDesc.Direction = float3( + // cos(2*PI*rnd.x)*sqrt(1-rnd.y*rnd.y), + // sin(2*PI*rnd.x)*sqrt(1-rnd.y*rnd.y), + // rnd.y + // ); + // continue; + //} + + //float p = max(max(mat.albedo.x, mat.albedo.y), mat.albedo.z); + //if(hitValue.depth > 5) { + // if (rnd.z >= p) return; + // else hitValue.accmat /= p; + //} + + + //-- Ideal DIFFUSE reflection + //if(bool(useNEE)) { + // accrad += nextEventEstimation(accmat, r.d, params.x, params.nl, kt, false, rnd); + //} + for(uint i = 0; i < pSamps.numDirectionalLights; ++i) { + float3 x = vert.position; + float3 l = -pParams.directionalLights[i].direction.xyz; + RayDesc rayDesc; + rayDesc.TMax = 10000.0f; + rayDesc.TMin = 0.001f; + rayDesc.Origin = x; + rayDesc.Direction = l; + RayPayload payload; + payload.depth = hitValue.depth; + payload.emissive = 1; + payload.anyHit = true; + TraceRay(pParams.scene, 0, 0xff, 0, 0, 0, rayDesc, payload); + + // we have missed all geometry, so directional light is affecting us + if(!payload.hit) { + localAccRad += mat.shade(vert.normal, -WorldRayDirection(), -pParams.directionalLights[i].direction, pParams.directionalLights[i].color); + } + } + for(uint i = 0; i < pSamps.numPointLights; ++i) { + RayPayload payload; + float3 x = vert.position; + float3 l = pParams.pointLights[i].position - vert.position; + // todo: cancel if light too far away to affect + + RayDesc rayDesc; + rayDesc.TMax = 1.0f; + rayDesc.TMin = 0.001f; + rayDesc.Origin = x; + rayDesc.Direction = l; + TraceRay(pParams.scene, 0, 0xff, 0, 0, 0, rayDesc, payload); + + // hitting only after the light + if(!payload.hit) { + localAccRad += mat.shade(vert.normal, -WorldRayDirection(), normalize(l), pParams.pointLights[i].color); + } + } + hitValue.light += localAccRad; + // Indirect Illumination: cosine-weighted importance sampling + if(hitValue.depth < 12) { + float r1 = 2 * PI * rnd.x, r2 = rnd.y, r2s = sqrt(r2); + float3 w = normalLight; + float3 u = normalize((cross(abs(w.x)>0.1 ? float3(0,1,0) : float3(1,0,0), w))); + float3 v = cross(w,u); + RayDesc rayDesc; + rayDesc.TMax = 10000.0f; + rayDesc.TMin = 0.001f; + rayDesc.Origin = vert.position; + rayDesc.Direction = normalize(u*cos(r1)*r2s + v * sin(r1)*r2s + w * sqrt(1 - r2)); + RayPayload payload; + payload.light = float3(0); + payload.emissive = 0; // in the next bounce, consider reflective part only! + payload.depth = hitValue.depth+1; + payload.anyHit = false; + TraceRay(pParams.scene, 0, 0xff, 0, 0, 0, rayDesc, payload); + } +} \ No newline at end of file diff --git a/res/shaders/Common.slang b/res/shaders/Common.slang new file mode 100644 index 0000000..3c6bf7e --- /dev/null +++ b/res/shaders/Common.slang @@ -0,0 +1,121 @@ +const static float PI = 3.1415926535897932f; + +struct Camera +{ + float3 cameraPosition; + float f; + float3 cameraForward; + float S_O; + float3 fogEmm; + float ks; + float A; + float ka; +}; + +struct MaterialParameter +{ + float3 albedo = float3(1, 1, 1); + float alpha = 1; + float3 specularColor = float3(1, 1, 1); + float shininess = 0.04; + float3 emissive = float3(0, 0, 0); + float3 shade(float3 normal, float3 viewDir, float3 lightDir, float3 lightColor) + { + float diffuse = max(dot(normal, lightDir), 0); + float3 h = normalize(lightDir + viewDir); + float specular = pow(clamp(dot(normal, h), 0, 1), shininess); + + return (albedo * diffuse * lightColor); + } +}; + +struct ModelReference +{ + uint32_t positionOffset = 0; + uint32_t indicesOffset = 0; + uint32_t numIndices = 0; +}; + +struct PointLight +{ + float3 position = float3(0, 0, 0); + float3 color = float3(1, 1, 1); + float attenuation = 1; +}; + +struct DirectionalLight +{ + float3 direction = float3(0, 1, 0); + float3 color = float3(1, 1, 1); +}; + +struct RaytracingParams +{ + Camera cam; + RaytracingAccelerationStructure scene; + RWTexture2D radianceAccumulator; + RWTexture2D image; + StructuredBuffer modelData; + StructuredBuffer materialData; + StructuredBuffer positions; + StructuredBuffer texCoords; + StructuredBuffer normals; + StructuredBuffer directionalLights; + StructuredBuffer pointLights; + StructuredBuffer indexBuffer; +}; +ParameterBlock pParams; + +struct Vertex +{ + float3 position; + float2 texCoords; + float3 normal; + static Vertex interpolate(Vertex f0, Vertex f1, Vertex f2, float3 barycentricCoords) + { + Vertex vert; + vert.position = f0.position * barycentricCoords.x + f1.position * barycentricCoords.y + f2.position * barycentricCoords.z; + vert.texCoords = f0.texCoords * barycentricCoords.x + f1.texCoords * barycentricCoords.y + f2.texCoords * barycentricCoords.z; + vert.normal = f0.normal * barycentricCoords.x + f1.normal * barycentricCoords.y + f2.normal * barycentricCoords.z; + return vert; + } +}; + +Vertex loadVertex(uint32_t vertexIndex) +{ + Vertex vert; + vert.position = float3(pParams.positions[vertexIndex * 3 + 0], pParams.positions[vertexIndex * 3 + 1], pParams.positions[vertexIndex * 3 + 2]); + vert.texCoords = float2(pParams.texCoords[vertexIndex * 2 + 0], pParams.texCoords[vertexIndex * 2 + 1]); + vert.normal = float3(pParams.normals[vertexIndex * 3 + 0], pParams.normals[vertexIndex * 3 + 1], pParams.normals[vertexIndex * 3 + 2]); + return vert; +} + +struct SampleParams +{ + uint pass; + uint samplesPerPixel; + uint numDirectionalLights; + uint numPointLights; +}; +layout(push_constant) +ConstantBuffer pSamps; + +struct Ray +{ + float3 o; + float3 d; +}; + +struct RayPayload +{ + float3 light; + float emissive; + uint depth; + bool hit; + bool anyHit; +}; + +float3 rand01(uint3 x){ // pseudo-random number generator + for (int i=3; i-->0;) x = ((x>>8U)^x.yzx)*1103515245U; + return float3(x)*(1.0/float(0xffffffffU)); +} \ No newline at end of file diff --git a/res/shaders/RayGen.slang b/res/shaders/RayGen.slang index e69de29..0a77fa5 100644 --- a/res/shaders/RayGen.slang +++ b/res/shaders/RayGen.slang @@ -0,0 +1,57 @@ +import Common; + +[shader("raygeneration")] +void raygen() +{ + if(pSamps.pass == pSamps.samplesPerPixel) return; + uint2 pix = DispatchRaysIndex().xy; + uint2 imgdim = DispatchRaysDimensions().xy; + + //-- define cam + Ray cam = Ray(pParams.cam.cameraPosition, pParams.cam.cameraForward); + float3 cx = -normalize(cross(cam.d, abs(cam.d.y) < 0.9 ? float3(0, 1, 0) : float3(0, 0, 1))), cy = cross(cam.d, cx); + const float2 sdim = float2(0.036, 0.024); + + float S_I = (pParams.cam.S_O * pParams.cam.f) / (pParams.cam.S_O - pParams.cam.f); + + //-- sample sensor + float2 rnd2 = 2*rand01(uint3(pix, pSamps.pass)).xy; // vvv tent filter sample + float2 tent = float2(rnd2.x<1 ? sqrt(rnd2.x)-1 : 1-sqrt(2-rnd2.x), rnd2.y<1 ? sqrt(rnd2.y)-1 : 1-sqrt(2-rnd2.y)); + float2 s = ((pix + 0.5 * (0.5 + float2((pSamps.pass/2)%2, pSamps.pass%2) + tent)) / float2(imgdim) - 0.5) * sdim; + float3 spos = cam.o + cx*s.x + cy*s.y, lc = cam.o + cam.d * 0.035; // sample on 3d sensor plane + Ray r = Ray(lc, normalize(lc - spos)); // construct ray + + + //-- setup lens + float3 lensP = lc; + float3 lensN = -cam.d; + float3 lensX = cross(lensN, float3(0, 1, 0)); // the exact vector doesnt matter + float3 lensY = cross(lensN, lensX); + uint3 rndSeed = uint3(pix, pSamps.pass); + float2 rnd01 = rand01(rndSeed).xy; + + float3 lensSample = lensP + rnd01.x * pParams.cam.A * lensX + rnd01.y * pParams.cam.A * lensY; + + float3 focalPoint = cam.o + (pParams.cam.S_O + S_I) * cam.d; + float t = dot(focalPoint - r.o, lensN) / dot(r.d, lensN); + float3 focus = r.o + t * r.d; + + RayDesc rayDesc; + rayDesc.Origin = lensSample; + rayDesc.Direction = normalize(focus - lensSample); + rayDesc.TMin = 0.001; + rayDesc.TMax = 10000.0; + + const uint maxDepth = 12; + RayPayload payload; + // initialize accumulated radiance and bxdf + payload.light=float3(0); + payload.emissive = 1; + payload.depth = 1; + payload.anyHit = false; + TraceRay(pParams.scene, 0, 0xff, 0, 0, 0, rayDesc, payload); + + if(pSamps.pass == 0) pParams.radianceAccumulator[pix] = float4(0); + pParams.radianceAccumulator[pix] += float4(payload.light / pSamps.samplesPerPixel, 0); + pParams.image[pix] = float4(clamp(pParams.radianceAccumulator[pix].xyz, 0, 1), 1); +} diff --git a/src/gpu/CMakeLists.txt b/src/gpu/CMakeLists.txt index c098f34..9d08211 100644 --- a/src/gpu/CMakeLists.txt +++ b/src/gpu/CMakeLists.txt @@ -1,4 +1,7 @@ target_sources(RayTracer PRIVATE GPURenderer.h - GPURenderer.cpp) \ No newline at end of file + GPURenderer.cpp + GPUScene.h + GPUScene.cpp +) \ No newline at end of file diff --git a/src/gpu/GPURenderer.cpp b/src/gpu/GPURenderer.cpp index 22c9566..0b5b39b 100644 --- a/src/gpu/GPURenderer.cpp +++ b/src/gpu/GPURenderer.cpp @@ -1,13 +1,20 @@ #include "GPURenderer.h" #include #include +#define VMA_IMPLEMENTATION +#include "vk_mem_alloc.h" 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) + closestHit(nullptr), miss(nullptr), pipeline(nullptr), radianceAccumulator(nullptr), radianceAllocation(nullptr), image(nullptr), + imageAllocation(nullptr) { + createDevice(); + createCommands(); + createDescriptors(); + createShaders(); } GPURenderer::~GPURenderer() {} @@ -43,6 +50,21 @@ void GPURenderer::createDevice() vk::DeviceQueueCreateInfo deviceQueueCreateInfo({}, computeQueueFamily, 1, &queuePriority); vk::DeviceCreateInfo deviceCreateInfo({}, deviceQueueCreateInfo); device = Device(physicalDevice, deviceCreateInfo); + + VmaVulkanFunctions vulkanFunctions = {}; + vulkanFunctions.vkGetInstanceProcAddr = &vkGetInstanceProcAddr; + vulkanFunctions.vkGetDeviceProcAddr = &vkGetDeviceProcAddr; + + VmaAllocatorCreateInfo allocatorCreateInfo = {}; + allocatorCreateInfo.flags = VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT; + allocatorCreateInfo.vulkanApiVersion = VK_API_VERSION_1_2; + allocatorCreateInfo.physicalDevice = *physicalDevice; + allocatorCreateInfo.device = *device; + allocatorCreateInfo.instance = *instance; + allocatorCreateInfo.pVulkanFunctions = &vulkanFunctions; + + VmaAllocator allocator; + vmaCreateAllocator(&allocatorCreateInfo, &allocator); } void GPURenderer::createCommands() @@ -57,8 +79,46 @@ void GPURenderer::createCommands() void GPURenderer::createDescriptors() { - vk::DescriptorSetLayoutBinding descriptorSetLayoutBinding(0, vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex); - vk::DescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo({}, descriptorSetLayoutBinding); + vk::DescriptorSetLayoutBinding bindings[] = { + // camera + vk::DescriptorSetLayoutBinding(0, vk::DescriptorType::eUniformBuffer, 1, + vk::ShaderStageFlagBits::eRaygenKHR | vk::ShaderStageFlagBits::eClosestHitKHR), + // scene acceleration structure + vk::DescriptorSetLayoutBinding(1, vk::DescriptorType::eAccelerationStructureKHR, 1, + vk::ShaderStageFlagBits::eRaygenKHR | vk::ShaderStageFlagBits::eClosestHitKHR), + // radiance accumulator + vk::DescriptorSetLayoutBinding(2, vk::DescriptorType::eStorageImage, 1, + vk::ShaderStageFlagBits::eRaygenKHR | vk::ShaderStageFlagBits::eClosestHitKHR), + // image + vk::DescriptorSetLayoutBinding(3, vk::DescriptorType::eStorageImage, 1, + vk::ShaderStageFlagBits::eRaygenKHR | vk::ShaderStageFlagBits::eClosestHitKHR), + // model data + vk::DescriptorSetLayoutBinding(4, vk::DescriptorType::eStorageBuffer, 1, + vk::ShaderStageFlagBits::eRaygenKHR | vk::ShaderStageFlagBits::eClosestHitKHR), + // material data + vk::DescriptorSetLayoutBinding(5, vk::DescriptorType::eStorageBuffer, 1, + vk::ShaderStageFlagBits::eRaygenKHR | vk::ShaderStageFlagBits::eClosestHitKHR), + // positions + vk::DescriptorSetLayoutBinding(6, vk::DescriptorType::eStorageBuffer, 1, + vk::ShaderStageFlagBits::eRaygenKHR | vk::ShaderStageFlagBits::eClosestHitKHR), + // texcoords + vk::DescriptorSetLayoutBinding(7, vk::DescriptorType::eStorageBuffer, 1, + vk::ShaderStageFlagBits::eRaygenKHR | vk::ShaderStageFlagBits::eClosestHitKHR), + // normals + vk::DescriptorSetLayoutBinding(8, vk::DescriptorType::eStorageBuffer, 1, + vk::ShaderStageFlagBits::eRaygenKHR | vk::ShaderStageFlagBits::eClosestHitKHR), + // directional lights + vk::DescriptorSetLayoutBinding(9, vk::DescriptorType::eStorageBuffer, 1, + vk::ShaderStageFlagBits::eRaygenKHR | vk::ShaderStageFlagBits::eClosestHitKHR), + // point lights + vk::DescriptorSetLayoutBinding(10, vk::DescriptorType::eStorageBuffer, 1, + vk::ShaderStageFlagBits::eRaygenKHR | vk::ShaderStageFlagBits::eClosestHitKHR), + // index buffer + vk::DescriptorSetLayoutBinding(11, vk::DescriptorType::eStorageBuffer, 1, + vk::ShaderStageFlagBits::eRaygenKHR | vk::ShaderStageFlagBits::eClosestHitKHR), + }; + + vk::DescriptorSetLayoutCreateInfo descriptorSetLayoutCreateInfo({}, bindings); descriptorLayout = DescriptorSetLayout(device, descriptorSetLayoutCreateInfo); // create a PipelineLayout using that DescriptorSetLayout @@ -108,4 +168,83 @@ void GPURenderer::createShaders() */ } -void GPURenderer::render(Camera cam, RenderParameter param) {} \ No newline at end of file +void GPURenderer::render(Camera cam, RenderParameter param) +{ + // camera + { + VkBufferCreateInfo bufferInfo = { + .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, + .size = sizeof(GPUCamera), + .usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT, + }; + VmaAllocationCreateInfo allocInfo = { + .flags = VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT, + .usage = VMA_MEMORY_USAGE_AUTO, + }; + + vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &cameraBuffer, &cameraAllocation, nullptr); + GPUCamera gpuCam = { + .cameraPosition = cam.position, + .f = cam.f, + .cameraForward = glm::normalize(cam.target - cam.position), + .S_O = cam.S_O, + .fogEmm = glm::vec3(0, 0, 0), + .ks = 0, + .A = cam.A, + .ka = 0, + }; + vmaCopyMemoryToAllocation(allocator, &gpuCam, cameraAllocation, 0, sizeof(GPUCamera)); + } + // radiance accumulator + { + VkImageCreateInfo imageInfo = { + .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, + .imageType = VK_IMAGE_TYPE_2D, + .format = VK_FORMAT_R32G32B32A32_SFLOAT, + .extent = + { + .width = (uint32_t)param.width, + .height = (uint32_t)param.height, + .depth = 1, + }, + .mipLevels = 1, + .arrayLayers = 1, + .samples = VK_SAMPLE_COUNT_1_BIT, + .tiling = VK_IMAGE_TILING_OPTIMAL, + .usage = VK_IMAGE_USAGE_STORAGE_BIT, + .initialLayout = VK_IMAGE_LAYOUT_GENERAL, + }; + + VmaAllocationCreateInfo allocCreateInfo = { + .flags = VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT, + .usage = VMA_MEMORY_USAGE_AUTO, + }; + vmaCreateImage(allocator, &imageInfo, &allocCreateInfo, &radianceAccumulator, &radianceAllocation, nullptr); + } + // image + { + VkImageCreateInfo imageInfo = { + .sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, + .imageType = VK_IMAGE_TYPE_2D, + .format = VK_FORMAT_R32G32B32A32_SFLOAT, + .extent = + { + .width = (uint32_t)param.width, + .height = (uint32_t)param.height, + .depth = 1, + }, + .mipLevels = 1, + .arrayLayers = 1, + .samples = VK_SAMPLE_COUNT_1_BIT, + .tiling = VK_IMAGE_TILING_OPTIMAL, + .usage = VK_IMAGE_USAGE_STORAGE_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT, + .initialLayout = VK_IMAGE_LAYOUT_GENERAL, + }; + + VmaAllocationCreateInfo allocCreateInfo = { + .flags = VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT, + .usage = VMA_MEMORY_USAGE_AUTO, + }; + vmaCreateImage(allocator, &imageInfo, &allocCreateInfo, &image, &imageAllocation, nullptr); + } +} \ No newline at end of file diff --git a/src/gpu/GPURenderer.h b/src/gpu/GPURenderer.h index 461b142..cc4fcc6 100644 --- a/src/gpu/GPURenderer.h +++ b/src/gpu/GPURenderer.h @@ -2,6 +2,7 @@ #include "scene/Renderer.h" #include #include +#include using namespace vk::raii; @@ -12,6 +13,17 @@ public: virtual ~GPURenderer(); private: + struct GPUCamera + { + glm::vec3 cameraPosition; + float f; + glm::vec3 cameraForward; + float S_O; + glm::vec3 fogEmm; + float ks; + float A; + float ka; + }; void createDevice(); void createCommands(); void createDescriptors(); @@ -22,6 +34,7 @@ private: PhysicalDevice physicalDevice; Device device; Queue queue; + VmaAllocator allocator; uint32_t computeQueueFamily; CommandPool cmdPool; @@ -38,5 +51,14 @@ private: Pipeline pipeline; + VkBuffer cameraBuffer; + VmaAllocation cameraAllocation; + + VkImage radianceAccumulator; + VmaAllocation radianceAllocation; + + VkImage image; + VmaAllocation imageAllocation; + virtual void render(Camera cam, RenderParameter param); }; \ No newline at end of file diff --git a/src/gpu/GPUScene.cpp b/src/gpu/GPUScene.cpp new file mode 100644 index 0000000..0c6c25e --- /dev/null +++ b/src/gpu/GPUScene.cpp @@ -0,0 +1,124 @@ +#include "GPUScene.h" + +GPUScene::~GPUScene() {} + +void GPUScene::generate() +{ + populateGeometryPools(); + + // upload geometry to gpu + createStorageBuffer(modelBuffer, modelAllocation, refs.data(), refs.size() * sizeof(ModelReference)); + // createStorageBuffer(materialBuffer, materialAllocation, refs.data(), refs.size() * sizeof(ModelReference)); + createStorageBuffer(positionBuffer, positionAllocation, positionPool.data(), positionPool.size() * sizeof(glm::vec3)); + createStorageBuffer(texCoordsBuffer, texCoordsAllocation, texCoordsPool.data(), texCoordsPool.size() * sizeof(glm::vec2)); + createStorageBuffer(normalsBuffer, normalsAllocation, normalsPool.data(), normalsPool.size() * sizeof(glm::vec3)); + createStorageBuffer(directionalLightBuffer, directionalLightAllocation, directionalLights.data(), + directionalLights.size() * sizeof(DirectionalLight)); + createStorageBuffer(pointLightBuffer, pointLightAllocation, pointLights.data(), pointLights.size() * sizeof(PointLight)); + createStorageBuffer(indexBuffer, indexAllocation, indicesPool.data(), indicesPool.size() * sizeof(glm::uvec3)); + + VkBufferDeviceAddressInfo addrInfo = { + .sType = VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO, + .buffer = positionBuffer, + }; + VkDeviceAddress vertexBufferAddr = vkGetBufferDeviceAddress(*device, &addrInfo); + addrInfo.buffer = indexBuffer; + VkDeviceAddress indexBufferAddr = vkGetBufferDeviceAddress(*device, &addrInfo); + + std::vector geometries(models.size()); + std::vector buildGeometries(models.size()); + std::vector buildSizes(models.size()); + std::vector scratchBuffers(models.size()); + std::vector scratchAllocations(models.size()); + std::vector buildRanges(models.size()); + std::vector buildRangePointers(models.size()); + blas.resize(models.size()); + for (uint32_t i = 0; i < models.size(); ++i) + { + vk::DeviceOrHostAddressConstKHR vertexDataAddress = (vertexBufferAddr + refs[i].positionOffset * sizeof(glm::vec3)); + vk::DeviceOrHostAddressConstKHR indexDataAddress = (indexBufferAddr + refs[i].indicesOffset + sizeof(glm::uvec3)); + + geometries[i] = vk::AccelerationStructureGeometryKHR( + vk::GeometryTypeKHR::eTriangles, + vk::AccelerationStructureGeometryTrianglesDataKHR(vk::Format::eR32G32B32Sfloat, vertexDataAddress, sizeof(glm::vec3), + (uint32_t)refs[i].numPositions, vk::IndexType::eUint32, indexDataAddress), + vk::GeometryFlagBitsKHR::eOpaque); + + buildGeometries[i] = vk::AccelerationStructureBuildGeometryInfoKHR( + vk::AccelerationStructureTypeKHR::eTopLevel, vk::BuildAccelerationStructureFlagBitsKHR::ePreferFastTrace, + vk::BuildAccelerationStructureModeKHR::eBuild, {}, {}, 1, &geometries[i], nullptr); + + buildSizes[i] = { + .sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_SIZES_INFO_KHR, + .pNext = nullptr, + }; + const uint32_t primitiveCount = refs[i].numIndices / 3; + buildSizes[i] = + device.getAccelerationStructureBuildSizesKHR(vk::AccelerationStructureBuildTypeKHR::eDevice, buildGeometries[i], primitiveCount); + + VkBufferCreateInfo bufferInfo = { + .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, + .pNext = nullptr, + .flags = 0, + .size = buildSizes[i].accelerationStructureSize, + .usage = VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, + }; + VmaAllocationCreateInfo bufferAllocInfo = { + .usage = VMA_MEMORY_USAGE_AUTO, + }; + vmaCreateBuffer(allocator, &bufferInfo, &bufferAllocInfo, &blas[i].buffer, &blas[i].alloc, nullptr); + + vk::AccelerationStructureCreateInfoKHR blasInfo({}, blas[i].buffer, 0, buildSizes[i].accelerationStructureSize, + vk::AccelerationStructureTypeKHR::eBottomLevel); + blas[i].handle = device.createAccelerationStructureKHR(blasInfo); + + VkBufferCreateInfo scratchInfo = { + .sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO, + .pNext = nullptr, + .flags = 0, + .size = buildSizes[i].buildScratchSize, + .usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT, + }; + VmaAllocationCreateInfo scratchAllocInfo = { + .usage = VMA_MEMORY_USAGE_AUTO, + }; + vmaCreateBufferWithAlignment(allocator, &scratchInfo, &scratchAllocInfo, 16, &scratchBuffers[i], &scratchAllocations[i], nullptr); + addrInfo.buffer = scratchBuffers[i]; + VkDeviceAddress scratchAddr = vkGetBufferDeviceAddress(*device, &addrInfo); + buildGeometries[i].dstAccelerationStructure = blas[i].handle; + buildGeometries[i].scratchData.deviceAddress = scratchAddr; + + buildRanges[i] = VkAccelerationStructureBuildRangeInfoKHR{ + .primitiveCount = primitiveCount, + .primitiveOffset = 0, + .firstVertex = 0, + .transformOffset = 0, + }; + buildRangePointers[i] = &buildRanges[i]; + } + vk::CommandBufferAllocateInfo commandBufferAllocateInfo(cmdPool, vk::CommandBufferLevel::ePrimary, 10); + CommandBuffer cmdBuffer = std::move(CommandBuffers(device, commandBufferAllocateInfo).front()); + vk::FenceCreateInfo fenceCreateInfo; + Fence fence = Fence(device, fenceCreateInfo); + cmdBuffer.begin(vk::CommandBufferBeginInfo(vk::CommandBufferUsageFlagBits::eOneTimeSubmit)); + cmdBuffer.buildAccelerationStructuresKHR(buildGeometries, buildRangePointers); + cmdBuffer.end(); + vk::SubmitInfo submitInfo; + queue.submit(submitInfo, fence); + assert(device.waitForFences({fence}, true, 1000000) == VK_SUCCESS); +} + +void GPUScene::createStorageBuffer(VkBuffer& buffer, VmaAllocation& alloc, void* data, size_t size) +{ + VkBufferCreateInfo bufferCreateInfo = {VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO}; + bufferCreateInfo.size = size; + bufferCreateInfo.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; + + VmaAllocationCreateInfo allocCreateInfo = {}; + allocCreateInfo.usage = VMA_MEMORY_USAGE_AUTO; + allocCreateInfo.flags = VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT; + + vmaCreateBuffer(allocator, &bufferCreateInfo, &allocCreateInfo, &buffer, &alloc, nullptr); + + vmaCopyMemoryToAllocation(allocator, data, alloc, 0, size); +} diff --git a/src/gpu/GPUScene.h b/src/gpu/GPUScene.h new file mode 100644 index 0000000..0af28f8 --- /dev/null +++ b/src/gpu/GPUScene.h @@ -0,0 +1,60 @@ +#pragma once +#include +#include +#include +#include "scene/Scene.h" + +using namespace vk::raii; + +class GPUScene : public Scene +{ +public: + GPUScene(Device& device, VmaAllocator& allocator, CommandPool& cmdPool); + virtual ~GPUScene(); + virtual void generate() override; + +private: + void createStorageBuffer(VkBuffer& buffer, VmaAllocation& alloc, void* data, size_t size); + + Device& device; + VmaAllocator& allocator; + CommandPool& cmdPool; + Queue& queue; + + // bottom level acceleration structure + struct BLAS + { + vk::AccelerationStructureKHR handle; + VkBuffer buffer; + VmaAllocation alloc; + }; + + AccelerationStructureKHR accelerationStructure; + VmaAllocation accelerationAllocation; + + std::vector blas; + + VkBuffer modelBuffer; + VmaAllocation modelAllocation; + + VkBuffer materialBuffer; + VmaAllocation materialAllocation; + + VkBuffer positionBuffer; + VmaAllocation positionAllocation; + + VkBuffer texCoordsBuffer; + VmaAllocation texCoordsAllocation; + + VkBuffer normalsBuffer; + VmaAllocation normalsAllocation; + + VkBuffer directionalLightBuffer; + VmaAllocation directionalLightAllocation; + + VkBuffer pointLightBuffer; + VmaAllocation pointLightAllocation; + + VkBuffer indexBuffer; + VmaAllocation indexAllocation; +}; \ No newline at end of file diff --git a/src/scene/Scene.cpp b/src/scene/Scene.cpp index a7571e3..60da458 100644 --- a/src/scene/Scene.cpp +++ b/src/scene/Scene.cpp @@ -21,28 +21,9 @@ void Scene::addModels(std::vector _models, glm::mat4 transform) void Scene::generate() { std::vector pendingNodes; - while (!models.empty()) + for (const auto& [model, ref] : std::views::zip(models, refs)) { - auto& model = models.back(); - ModelReference ref = { - .positionOffset = (uint32_t)positionPool.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]); - } - for (uint32_t i = 0; i < model->indices.size(); ++i) - { - indicesPool.push_back(model->indices[i]); - edgesPool.push_back(model->edges[i * 2 + 0]); - edgesPool.push_back(model->edges[i * 2 + 1]); - faceNormalsPool.push_back(glm::normalize(model->faceNormals[i])); - } pendingNodes.push_back(std::make_unique(model->boundingBox, ref)); - models.pop_back(); } while (pendingNodes.size() > 1) { @@ -138,6 +119,35 @@ void Scene::traceRay(Ray ray, Payload& payload, const float tmin, const float tm } } +void Scene::populateGeometryPools() +{ + //todo: clear everything + for(uint32_t i = 0; i < models.size(); ++i) + { + auto& model = models[i]; + 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) + { + indicesPool.push_back(model->indices[i]); + edgesPool.push_back(model->edges[i * 2 + 0]); + edgesPool.push_back(model->edges[i * 2 + 1]); + faceNormalsPool.push_back(glm::normalize(model->faceNormals[i])); + } + refs.push_back(ref); + } +} + bool Scene::testIntersection(const PNode& currentNode, const Ray ray, const float tmin, float tmax) const noexcept { if (!currentNode->aabb.intersects(ray, tmin, tmax)) diff --git a/src/scene/Scene.h b/src/scene/Scene.h index 1cb610c..585c437 100644 --- a/src/scene/Scene.h +++ b/src/scene/Scene.h @@ -9,6 +9,7 @@ struct ModelReference { uint32_t positionOffset = 0; + uint32_t numPositions = 0; uint32_t indicesOffset = 0; uint32_t numIndices = 0; }; @@ -16,6 +17,7 @@ struct ModelReference struct PointLight { glm::vec3 position = glm::vec3(0, 0, 0); + float pad; glm::vec3 color = glm::vec3(1, 1, 1); float attenuation = 1; }; @@ -23,7 +25,9 @@ struct PointLight struct DirectionalLight { glm::vec3 direction = glm::vec3(0, 1, 0); + float pad; glm::vec3 color = glm::vec3(1, 1, 1); + float pad1; }; class Scene @@ -33,13 +37,15 @@ public: void addDirectionalLight(DirectionalLight dir) { directionalLights.push_back(dir); } void addModel(PModel model, glm::mat4 transform); void addModels(std::vector models, glm::mat4 transform); - void generate(); + virtual void generate(); void traceRay(Ray ray, Payload& payload, const float tmin, const float tmax) const noexcept; -private: +protected: + std::vector refs; std::vector positionPool; std::vector texCoordsPool; + std::vector normalsPool; std::vector indicesPool; std::vector edgesPool; std::vector faceNormalsPool; @@ -60,6 +66,8 @@ private: PNode hierarchy; std::vector models; + void populateGeometryPools(); + // 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; IntersectionInfo generateIntersections(const PNode& currentNode, const Ray ray, const float tmin, const float tmax) const noexcept; diff --git a/src/util/Model.cpp b/src/util/Model.cpp index aa73bc1..dfc3785 100644 --- a/src/util/Model.cpp +++ b/src/util/Model.cpp @@ -7,6 +7,11 @@ void Model::transform(glm::mat4 matrix) pos = glm::vec3(matrix * glm::vec4(pos, 1)); } + for (auto& nor : normals) + { + nor = glm::mat3(matrix) * nor; + } + boundingBox.transform(matrix); for (int i = 0; i < indices.size(); i++) diff --git a/src/util/Model.h b/src/util/Model.h index 21364c4..7148048 100644 --- a/src/util/Model.h +++ b/src/util/Model.h @@ -16,6 +16,7 @@ public: AABB boundingBox; std::vector positions; std::vector texCoords; + std::vector normals; std::vector indices; std::vector edges; std::vector faceNormals; diff --git a/vcpkg.json b/vcpkg.json index e880352..0a8b6aa 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -5,6 +5,7 @@ "features": [ "glfw-binding", "opengl3-binding" ] }, "vulkan", + "vulkan-memory-allocator", "assimp", "ktx", "glfw3",