Ray tracing texturing and alpha mapping sample

This commit is contained in:
Sascha Willems 2023-04-09 13:40:12 +02:00
parent d0ad204606
commit cd6e3b9a23
2 changed files with 25 additions and 12 deletions

View file

@ -397,7 +397,12 @@ namespace vks
uint32_t alignedSize(uint32_t value, uint32_t alignment) uint32_t alignedSize(uint32_t value, uint32_t alignment)
{ {
return (value + alignment - 1) & ~(alignment - 1); return (value + alignment - 1) & ~(alignment - 1);
} }
size_t alignedSize(size_t value, size_t alignment)
{
return (value + alignment - 1) & ~(alignment - 1);
}
} }
} }

View file

@ -1,12 +1,21 @@
/* /*
* Vulkan Example - Texture mapping with transparency using accelerated ray tracing example * Vulkan Example - Texture mapping with transparency using accelerated ray tracing example
* *
* Copyright (C) 2023 by Sascha Willems - www.saschawillems.de * Copyright (C) 2023 by Sascha Willems - www.saschawillems.de
* *
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) * This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT)
*/ */
// @todo: add some comment on how this sample works /*
* This hardware accelerated ray tracing sample renders a texture mapped quad with transparency
* The sample also makes use of buffer device addresses to pass references for vertex and index buffers
* to the shader, making data access a bit more straightforward than using descriptors.
* Buffer references themselves are then simply set at draw time using push constants.
* In addition to a closest hit shader, that now samples from the texture, an any hit shader is
* added to the closest hit shader group. We use this shader to check if the texel we want to
* sample at the currently hit ray position is transparent, and if that's the case the any hit
* shader will cancel the intersection.
*/
#include "VulkanRaytracingSample.h" #include "VulkanRaytracingSample.h"
@ -49,8 +58,7 @@ public:
camera.setRotation(glm::vec3(45.0f, 0.0f, 0.0f)); camera.setRotation(glm::vec3(45.0f, 0.0f, 0.0f));
camera.setTranslation(glm::vec3(0.0f, 0.0f, -1.0f)); camera.setTranslation(glm::vec3(0.0f, 0.0f, -1.0f));
enableExtensions(); enableExtensions();
// Buffer device address requires the 64-bit integer feature to be enabled
// @todo
enabledFeatures.shaderInt64 = VK_TRUE; enabledFeatures.shaderInt64 = VK_TRUE;
} }
@ -157,7 +165,6 @@ public:
// Build // Build
VkAccelerationStructureGeometryKHR accelerationStructureGeometry{}; VkAccelerationStructureGeometryKHR accelerationStructureGeometry{};
accelerationStructureGeometry.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR; accelerationStructureGeometry.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_KHR;
//accelerationStructureGeometry.flags = VK_GEOMETRY_OPAQUE_BIT_KHR; // @todo: do not enable if anyhit shader is used
accelerationStructureGeometry.geometryType = VK_GEOMETRY_TYPE_TRIANGLES_KHR; accelerationStructureGeometry.geometryType = VK_GEOMETRY_TYPE_TRIANGLES_KHR;
accelerationStructureGeometry.geometry.triangles.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_TRIANGLES_DATA_KHR; accelerationStructureGeometry.geometry.triangles.sType = VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_TRIANGLES_DATA_KHR;
accelerationStructureGeometry.geometry.triangles.vertexFormat = VK_FORMAT_R32G32B32_SFLOAT; accelerationStructureGeometry.geometry.triangles.vertexFormat = VK_FORMAT_R32G32B32_SFLOAT;
@ -577,7 +584,8 @@ public:
bufferReferences.vertices = getBufferDeviceAddress(vertexBuffer.buffer); bufferReferences.vertices = getBufferDeviceAddress(vertexBuffer.buffer);
bufferReferences.indices = getBufferDeviceAddress(indexBuffer.buffer); bufferReferences.indices = getBufferDeviceAddress(indexBuffer.buffer);
// @todo: comment // We set the buffer references for the mesh to be rendered using a push constant
// If we wanted to render multiple objecets this would make it very easy to access their vertex and index buffers
vkCmdPushConstants(drawCmdBuffers[i], pipelineLayout, VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR | VK_SHADER_STAGE_ANY_HIT_BIT_KHR, 0, sizeof(uint64_t) * 2, &bufferReferences); vkCmdPushConstants(drawCmdBuffers[i], pipelineLayout, VK_SHADER_STAGE_CLOSEST_HIT_BIT_KHR | VK_SHADER_STAGE_ANY_HIT_BIT_KHR, 0, sizeof(uint64_t) * 2, &bufferReferences);
VkStridedDeviceAddressRegionKHR emptySbtEntry = {}; VkStridedDeviceAddressRegionKHR emptySbtEntry = {};