forked from KhronosGroup/Vulkan-Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraytracing_basic.h
More file actions
106 lines (91 loc) · 3.55 KB
/
raytracing_basic.h
File metadata and controls
106 lines (91 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* Copyright (c) 2019-2021, Sascha Willems
*
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Basic example for hardware accelerated ray tracing using VK_KHR_ray_tracing_pipeline and VK_KHR_acceleration_structure
*/
#pragma once
#include "api_vulkan_sample.h"
#include "glsl_compiler.h"
// Holds data for a scratch buffer used as a temporary storage during acceleration structure builds
struct ScratchBuffer
{
uint64_t device_address;
VkBuffer handle;
VkDeviceMemory memory;
};
// Wraps all data required for an acceleration structure
struct AccelerationStructure
{
VkAccelerationStructureKHR handle;
uint64_t device_address;
std::unique_ptr<vkb::core::Buffer> buffer;
};
class RaytracingBasic : public ApiVulkanSample
{
public:
VkPhysicalDeviceRayTracingPipelinePropertiesKHR ray_tracing_pipeline_properties{};
VkPhysicalDeviceAccelerationStructureFeaturesKHR acceleration_structure_features{};
AccelerationStructure bottom_level_acceleration_structure;
AccelerationStructure top_level_acceleration_structure;
std::unique_ptr<vkb::core::Buffer> vertex_buffer;
std::unique_ptr<vkb::core::Buffer> index_buffer;
uint32_t index_count;
std::vector<VkRayTracingShaderGroupCreateInfoKHR> shader_groups{};
std::unique_ptr<vkb::core::Buffer> raygen_shader_binding_table;
std::unique_ptr<vkb::core::Buffer> miss_shader_binding_table;
std::unique_ptr<vkb::core::Buffer> hit_shader_binding_table;
struct StorageImage
{
VkDeviceMemory memory;
VkImage image = VK_NULL_HANDLE;
VkImageView view;
VkFormat format;
uint32_t width;
uint32_t height;
} storage_image;
struct UniformData
{
glm::mat4 view_inverse;
glm::mat4 proj_inverse;
} uniform_data;
std::unique_ptr<vkb::core::Buffer> ubo;
VkPipeline pipeline;
VkPipelineLayout pipeline_layout;
VkDescriptorSet descriptor_set;
VkDescriptorSetLayout descriptor_set_layout;
RaytracingBasic();
~RaytracingBasic();
void request_gpu_features(vkb::PhysicalDevice &gpu) override;
uint64_t get_buffer_device_address(VkBuffer buffer);
ScratchBuffer create_scratch_buffer(VkDeviceSize size);
void delete_scratch_buffer(ScratchBuffer &scratch_buffer);
void create_storage_image();
void create_bottom_level_acceleration_structure();
void create_top_level_acceleration_structure();
void delete_acceleration_structure(AccelerationStructure &acceleration_structure);
void create_scene();
void create_shader_binding_tables();
void create_descriptor_sets();
void create_ray_tracing_pipeline();
void create_uniform_buffer();
void build_command_buffers() override;
void update_uniform_buffers();
void draw();
bool prepare(vkb::Platform &platform) override;
virtual void render(float delta_time) override;
};
std::unique_ptr<vkb::VulkanSample> create_raytracing_basic();