Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements in ray tracing support #13

Merged
merged 1 commit into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions include/fwk/gfx/shader_compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ struct ShaderCompilerSetup {
vector<FilePath> source_dirs;
Maybe<FilePath> spirv_cache_dir;
VulkanVersion vulkan_version = {1, 2, 0};
Maybe<double> glsl_version = 4.5;
Maybe<double> spirv_version = 1.3;
Maybe<double> spirv_version = 1.5;
bool debug_info = false;
bool generate_assembly = false;
};
Expand Down
1 change: 1 addition & 0 deletions include/fwk/vulkan/vulkan_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ inline auto toVk(VDescriptorType type) {
return type == VDescriptorType::accel_struct ? VK_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR :
VkDescriptorType(type);
}

inline auto toVk(VDescriptorPoolFlags flags) { return VkDescriptorPoolCreateFlagBits(flags.bits); }
inline auto toVk(VPrimitiveTopology type) { return VkPrimitiveTopology(type); }
inline auto toVk(VImageUsageFlags usage) { return VkImageUsageFlags{usage.bits}; }
Expand Down
1 change: 1 addition & 0 deletions include/fwk/vulkan/vulkan_ray_tracing.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class VulkanAccelStruct : public VulkanObjectBase<VulkanAccelStruct> {
~VulkanAccelStruct();

PVBuffer m_buffer;
PVBuffer m_scratch_buffer;
};

}
2 changes: 1 addition & 1 deletion include/fwk/vulkan_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ DEFINE_ENUM(VBindPoint, graphics, compute);

DEFINE_ENUM(VBufferUsage, transfer_src, transfer_dst, uniform_texel_buffer, storage_texel_buffer,
uniform_buffer, storage_buffer, index_buffer, vertex_buffer, indirect_buffer,
device_address, accel_struct_build_input, accel_struct_storage);
device_address, accel_struct_build_input_read_only, accel_struct_storage);
using VBufferUsageFlags = EnumFlags<VBufferUsage>;

DEFINE_ENUM(VImageUsage, transfer_src, transfer_dst, sampled, storage, color_att, depth_stencil_att,
Expand Down
4 changes: 1 addition & 3 deletions src/gfx/shader_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,7 @@ ShaderCompiler::ShaderCompiler(ShaderCompilerSetup setup) {
}
uint vk_version = (setup.vulkan_version.major << 22) | (setup.vulkan_version.minor << 12);
shaderc_compile_options_set_target_env(opts, shaderc_target_env_vulkan, vk_version);
if(setup.glsl_version)
shaderc_compile_options_set_forced_version_profile(opts, int(*setup.glsl_version * 100),
shaderc_profile_none);

if(setup.spirv_version) {
int version = int(*setup.spirv_version * 10);
int major = version / 10;
Expand Down
6 changes: 3 additions & 3 deletions src/vulkan/vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ Ex<void> VulkanDevice::initialize(const VDeviceSetup &setup) {
m_features |= VDeviceFeature::shader_clock;
}

vector<string> raytracing_exts{{VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME,
VK_KHR_RAY_TRACING_PIPELINE_EXTENSION_NAME,
VK_KHR_DEFERRED_HOST_OPERATIONS_EXTENSION_NAME}};
vector<string> raytracing_exts{
{VK_KHR_ACCELERATION_STRUCTURE_EXTENSION_NAME, VK_KHR_RAY_TRACING_PIPELINE_EXTENSION_NAME,
VK_KHR_DEFERRED_HOST_OPERATIONS_EXTENSION_NAME, VK_KHR_RAY_QUERY_EXTENSION_NAME}};
if(allOf(raytracing_exts,
[&](const string &ext) { return anyOf(phys_info.extensions, ext); })) {
insertBack(exts, raytracing_exts);
Expand Down
2 changes: 1 addition & 1 deletion src/vulkan/vulkan_pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ void VDescriptorSet::set(int index, PVAccelStruct as) {
write.dstSet = handle;
write.dstBinding = index;
write.dstArrayElement = 0;
write.descriptorType = toVk(VDescriptorType::storage_image);
write.descriptorType = toVk(VDescriptorType::accel_struct);
write.pNext = &as_info;
vkUpdateDescriptorSets(device->handle(), 1, &write, 0, nullptr);
}
Expand Down
48 changes: 31 additions & 17 deletions src/vulkan/vulkan_ray_tracing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Ex<PVAccelStruct> VulkanAccelStruct::create(VulkanDevice &device, VAccelStructTy
VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_CREATE_INFO_KHR};
create_info.buffer = buffer.buffer().handle();
create_info.offset = buffer.byteOffset();
create_info.size = buffer.byteOffset();
create_info.size = buffer.byteSize();
create_info.type = VkAccelerationStructureTypeKHR(type);

VkAccelerationStructureKHR handle = 0;
Expand All @@ -37,14 +37,21 @@ Ex<PVAccelStruct> VulkanAccelStruct::create(VulkanDevice &device, VAccelStructTy
return device.createObject(handle, buffer.buffer());
}

template <class T> static u64 getAddress(VulkanDevice &device, VBufferSpan<T> buffer) {
static u64 getAddress(VulkanDevice &device, const VulkanBuffer &buffer) {
VkBufferDeviceAddressInfo info = {VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO};
info.buffer = buffer.buffer().handle();
return vkGetBufferDeviceAddress(device.handle(), &info) + buffer.byteOffset();
info.buffer = buffer.handle();
return vkGetBufferDeviceAddress(device.handle(), &info);
}

template <class T> static u64 getAddress(VulkanDevice &device, VBufferSpan<T> buffer) {
return getAddress(device, *buffer.buffer()) + buffer.byteOffset();
}

Ex<PVAccelStruct> VulkanAccelStruct::build(VulkanDevice &device, VBufferSpan<float3> vertices,
VBufferSpan<u32> indices) {
DASSERT(vertices.buffer()->usage() & VBufferUsage::accel_struct_build_input_read_only);
DASSERT(indices.buffer()->usage() & VBufferUsage::accel_struct_build_input_read_only);

VkAccelerationStructureGeometryTrianglesDataKHR triangles{
VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_GEOMETRY_TRIANGLES_DATA_KHR};
triangles.vertexFormat = VK_FORMAT_R32G32B32_SFLOAT;
Expand All @@ -61,11 +68,12 @@ Ex<PVAccelStruct> VulkanAccelStruct::build(VulkanDevice &device, VBufferSpan<flo
geometry.flags = VK_GEOMETRY_OPAQUE_BIT_KHR;
geometry.geometry.triangles = triangles;

VkAccelerationStructureBuildRangeInfoKHR offset;
offset.firstVertex = vertices.byteOffset() / sizeof(float3);
offset.primitiveCount = indices.size() / 3;
offset.primitiveOffset = 0;
offset.transformOffset = 0;
VkAccelerationStructureBuildRangeInfoKHR build_range_info;
build_range_info.firstVertex = vertices.byteOffset() / sizeof(float3);
build_range_info.primitiveCount = indices.size() / 3;
build_range_info.primitiveOffset = 0;
build_range_info.transformOffset = 0;
auto *build_range_infos = &build_range_info;

VkAccelerationStructureBuildGeometryInfoKHR build_info{
VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_GEOMETRY_INFO_KHR};
Expand All @@ -79,22 +87,28 @@ Ex<PVAccelStruct> VulkanAccelStruct::build(VulkanDevice &device, VBufferSpan<flo
VK_STRUCTURE_TYPE_ACCELERATION_STRUCTURE_BUILD_SIZES_INFO_KHR};
uint32_t prim_count = indices.size() / 3;

vkGetAccelerationStructureBuildSizesKHR(
device.handle(),
VkAccelerationStructureBuildTypeKHR::VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR,
&build_info, &prim_count, &size_info);
vkGetAccelerationStructureBuildSizesKHR(device.handle(),
VK_ACCELERATION_STRUCTURE_BUILD_TYPE_DEVICE_KHR,
&build_info, &prim_count, &size_info);

auto buffer = EX_PASS(VulkanBuffer::create(device, size_info.accelerationStructureSize,
VBufferUsage::accel_struct_storage));
auto accel = EX_PASS(VulkanAccelStruct::create(device, VAccelStructType::bottom_level, buffer));

accel->m_scratch_buffer =
EX_PASS(VulkanBuffer::create(device, size_info.buildScratchSize,
VBufferUsage::device_address | VBufferUsage::storage_buffer));
build_info.scratchData.deviceAddress = getAddress(device, *accel->m_scratch_buffer);

build_info.srcAccelerationStructure = accel->handle();
build_info.dstAccelerationStructure = accel->handle();

auto *offsets = &offset;

auto result =
vkBuildAccelerationStructuresKHR(device.handle(), nullptr, 1, &build_info, &offsets);
auto &cmds = device.cmdQueue();
auto cmd_handle = cmds.bufferHandle();
vkCmdBuildAccelerationStructuresKHR(cmd_handle, 1, &build_info, &build_range_infos);
// TODO: barriers here in case of multiple builds
// TODO: compacting
// TODO: clear scratch buffer once building is done

return accel;
}
Expand Down
13 changes: 8 additions & 5 deletions src/vulkan/vulkan_shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ Ex<vector<VDescriptorBindingInfo>> getBindings(CSpan<char> bytecode, VShaderStag

for(int i : intRange(reflection->descriptor_binding_count)) {
auto &binding = reflection->descriptor_bindings[i];
uint desc_type = uint(binding.descriptor_type);
if(desc_type > count<VDescriptorType>)
return ERROR("Unsupported descriptor type: %", desc_type);
out.emplace_back(VDescriptorType(desc_type), flag(stage), binding.binding, binding.count,
binding.set);
VDescriptorType desc_type;
if(binding.descriptor_type <= SPV_REFLECT_DESCRIPTOR_TYPE_INPUT_ATTACHMENT)
desc_type = VDescriptorType(binding.descriptor_type);
else if(binding.descriptor_type == SPV_REFLECT_DESCRIPTOR_TYPE_ACCELERATION_STRUCTURE_KHR)
desc_type = VDescriptorType::accel_struct;
else
return ERROR("Unsupported descriptor type: %", binding.descriptor_type);
out.emplace_back(desc_type, flag(stage), binding.binding, binding.count, binding.set);
}

// TODO: is this needed?
Expand Down
1 change: 1 addition & 0 deletions todo.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CAŁY CZAS TO ROBISZ :(


Problemy:
- natstepfilter
- remove min_size from Span<>
- disable/remove backtraces?
- drobny błąd przy wyświetlaniu perfanalyzera: jak sie zmniejszy okno, to ostatnia linia moze byc czasami
Expand Down
Loading