Skip to content

Commit 8010262

Browse files
committed
Implementing Acceleration Structures
1 parent 2839096 commit 8010262

33 files changed

+589
-155
lines changed

include/engine/common.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#ifdef _WIN32
1313
// Windows-specific includes and definitions
1414
#define VK_USE_PLATFORM_WIN32_KHR
15+
// #include <Volk/volk.h>
16+
// #define VK_NO_PROTOTYPES
1517
#define GLFW_INCLUDE_VULKAN
1618
#include <GLFW/glfw3.h>
1719
#define GLFW_EXPOSE_NATIVE_WIN32
@@ -540,9 +542,9 @@ typedef enum RenderResult
540542
THREAD_DONE_KHR = VK_THREAD_DONE_KHR,
541543
OPERATION_DEFERRED_KHR = VK_OPERATION_DEFERRED_KHR,
542544
OPERATION_NOT_DEFERRED_KHR = VK_OPERATION_NOT_DEFERRED_KHR,
543-
ERROR_INVALID_VIDEO_STD_PARAMETERS_KHR = VK_ERROR_INVALID_VIDEO_STD_PARAMETERS_KHR,
545+
// ERROR_INVALID_VIDEO_STD_PARAMETERS_KHR = VK_ERROR_INVALID_VIDEO_STD_PARAMETERS_KHR,
544546
ERROR_COMPRESSION_EXHAUSTED_EXT = VK_ERROR_COMPRESSION_EXHAUSTED_EXT,
545-
INCOMPATIBLE_SHADER_BINARY_EXT = VK_INCOMPATIBLE_SHADER_BINARY_EXT,
547+
// INCOMPATIBLE_SHADER_BINARY_EXT = VK_INCOMPATIBLE_SHADER_BINARY_EXT,
546548
ERROR_OUT_OF_POOL_MEMORY_KHR = VK_ERROR_OUT_OF_POOL_MEMORY_KHR,
547549
ERROR_INVALID_EXTERNAL_HANDLE_KHR = VK_ERROR_INVALID_EXTERNAL_HANDLE_KHR,
548550
ERROR_FRAGMENTATION_EXT = VK_ERROR_FRAGMENTATION_EXT,

include/engine/core/geometries/geometry.h

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,17 @@
1111
#define GEOMETRY_H
1212

1313
#include <engine/common.h>
14-
#include <engine/graphics/buffer.h>
14+
#include <engine/graphics/accel.h>
15+
#include <engine/graphics/vao.h>
1516

1617
VULKAN_ENGINE_NAMESPACE_BEGIN
1718

18-
namespace Core
19-
{
19+
namespace Core {
2020

2121
class Geometry;
2222

23-
struct GeometricData
24-
{
25-
std::vector<uint32_t> vertexIndex;
23+
struct GeometricData {
24+
std::vector<uint32_t> vertexIndex;
2625
std::vector<Graphics::Utils::Vertex> vertexData;
2726

2827
// Stats
@@ -35,7 +34,6 @@ struct GeometricData
3534
void compute_statistics();
3635
};
3736

38-
3937
/*
4038
Class that defines the mesh geometry. Can be setup by filling it with a canonical vertex type array.
4139
*/
@@ -44,55 +42,49 @@ class Geometry
4442

4543
private:
4644
Graphics::VertexArrays m_VAO{};
47-
GeometricData m_geometryData{};
45+
Graphics::BLAS m_BLAS{};
46+
GeometricData m_geometryData{};
4847

4948
size_t m_materialID{0};
5049

51-
friend Graphics::VertexArrays *const get_render_data(Geometry *g);
50+
friend Graphics::VertexArrays* const get_VAO(Geometry* g);
51+
friend Graphics::BLAS* const get_BLAS(Geometry* g);
5252

5353
public:
54-
Geometry()
55-
{
54+
Geometry() {
5655
}
5756

58-
inline size_t get_material_ID() const
59-
{
57+
inline size_t get_material_ID() const {
6058
return m_materialID;
6159
}
62-
inline void set_material_ID(size_t id)
63-
{
60+
inline void set_material_ID(size_t id) {
6461
m_materialID = id;
6562
}
6663

67-
inline bool data_loaded() const
68-
{
64+
inline bool data_loaded() const {
6965
return m_geometryData.loaded;
7066
}
71-
inline bool indexed() const
72-
{
67+
inline bool indexed() const {
7368
return !m_geometryData.vertexIndex.empty();
7469
}
7570

76-
inline const GeometricData *get_geometric_data() const
77-
{
71+
inline const GeometricData* get_geometric_data() const {
7872
return &m_geometryData;
7973
}
80-
~Geometry()
81-
{
74+
~Geometry() {
8275
}
8376

84-
void fill(std::vector<Graphics::Utils::Vertex> vertexInfo);
85-
void fill(std::vector<Graphics::Utils::Vertex> vertexInfo, std::vector<uint32_t> vertexIndex);
86-
void fill(Vec3 *pos, Vec3 *normal, Vec2 *uv, Vec3 *tangent, uint32_t vertNumber);
87-
static Geometry *create_quad();
88-
static Geometry *create_cube();
77+
void fill(std::vector<Graphics::Utils::Vertex> vertexInfo);
78+
void fill(std::vector<Graphics::Utils::Vertex> vertexInfo, std::vector<uint32_t> vertexIndex);
79+
void fill(Vec3* pos, Vec3* normal, Vec2* uv, Vec3* tangent, uint32_t vertNumber);
80+
static Geometry* create_quad();
81+
static Geometry* create_cube();
8982
};
9083

91-
Graphics::VertexArrays *const get_render_data(Geometry *g);
84+
Graphics::VertexArrays* const get_VAO(Geometry* g);
9285

9386
} // namespace Core
9487

9588
VULKAN_ENGINE_NAMESPACE_END;
9689

97-
9890
#endif // VK_GEOMETRY_H

include/engine/core/materials/hair_strand.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ class HairStrandMaterial : public IMaterial
2626
float m_thickness{0.003f};
2727

2828
bool m_R{true}; // Reflection
29-
float m_Rpower{4.0f};
29+
float m_Rpower{1.0f};
3030

3131
bool m_TT{true}; // Transmitance
32-
float m_TTpower{5.0f};
32+
float m_TTpower{1.0f};
3333

3434
bool m_TRT{true}; // Second reflection
35-
float m_TRTpower{10.0f};
35+
float m_TRTpower{3.0f};
3636

3737
float m_roughness{0.4f};
3838
float m_scatter{500.0f};

include/engine/graphics/accel.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
This file is part of Vulkan-Engine, a simple to use Vulkan based 3D library
3+
4+
MIT License
5+
6+
Copyright (c) 2023 Antonio Espinosa Garcia
7+
8+
*/
9+
#ifndef ACCEL_H
10+
#define ACCEL_H
11+
12+
#include <engine/common.h>
13+
#include <engine/graphics/utilities/initializers.h>
14+
#include <engine/graphics/extensions.h>
15+
#include <engine/graphics/vao.h>
16+
17+
VULKAN_ENGINE_NAMESPACE_BEGIN
18+
19+
namespace Graphics {
20+
/*
21+
Bottom Level Acceleration structure
22+
*/
23+
struct BLAS {
24+
VkAccelerationStructureKHR handle = VK_NULL_HANDLE;
25+
VkDevice device = VK_NULL_HANDLE;
26+
VkDeviceAddress deviceAdress = 0;
27+
28+
Buffer buffer = {};
29+
30+
public:
31+
BLAS() {
32+
}
33+
34+
void cleanup();
35+
};
36+
/*
37+
Top Level Acceleration structure
38+
*/
39+
struct TLAS {
40+
VkAccelerationStructureKHR handle = VK_NULL_HANDLE;
41+
VkDevice device = VK_NULL_HANDLE;
42+
VkDeviceAddress deviceAdress = 0;
43+
44+
Buffer buffer = {};
45+
46+
public:
47+
TLAS() {
48+
}
49+
50+
void cleanup();
51+
};
52+
53+
} // namespace Graphics
54+
55+
VULKAN_ENGINE_NAMESPACE_END
56+
57+
#endif

include/engine/graphics/buffer.h

Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,50 +15,38 @@ VULKAN_ENGINE_NAMESPACE_BEGIN
1515

1616
namespace Graphics {
1717
// Vulkan buffer object
18-
class Buffer
19-
{
20-
VkBuffer m_handle;
18+
struct Buffer {
19+
VkBuffer handle = VK_NULL_HANDLE;
2120
/*Memory allocation controlled by VMA*/
22-
VmaAllocator m_memory;
23-
VmaAllocation m_allocation;
21+
VmaAllocator memory = VK_NULL_HANDLE;
22+
VmaAllocation allocation = VK_NULL_HANDLE;
23+
uint32_t size = 0;
24+
uint32_t strideSize = 0;
25+
std::vector<uint32_t> partitionsSize;
2426

25-
uint32_t m_strideSize{0};
26-
std::vector<uint32_t> m_partitionsSizes;
27-
28-
public:
29-
inline VkBuffer get_handle() const {
30-
return m_handle;
31-
}
32-
inline uint32_t get_stride_size() const {
33-
return m_strideSize;
27+
Buffer() {
3428
}
35-
Buffer() {}
3629

37-
void init(VmaAllocator& memory, size_t allocSize, VkBufferUsageFlags usage, VmaMemoryUsage memoryUsage,
38-
uint32_t istrideSize = 0);
39-
void init(VmaAllocator& memory, size_t allocSize, VkBufferUsageFlags usage, VmaMemoryUsage memoryUsage,
40-
uint32_t istrideSize, std::vector<uint32_t> stridePartitionsSizes);
30+
void init(VmaAllocator& _memory,
31+
size_t allocSize,
32+
VkBufferUsageFlags usage,
33+
VmaMemoryUsage memoryUsage,
34+
uint32_t istrideSize = 0);
35+
void init(VmaAllocator& _memory,
36+
size_t allocSize,
37+
VkBufferUsageFlags usage,
38+
VmaMemoryUsage memoryUsage,
39+
uint32_t istrideSize,
40+
std::vector<uint32_t> stridePartitionsSizes);
4141

4242
void upload_data(const void* bufferData, size_t size);
4343

4444
void upload_data(const void* bufferData, size_t size, size_t offset);
4545

4646
void cleanup();
47-
};
48-
49-
/*
50-
Geometric Render Data
51-
*/
52-
struct VertexArrays {
53-
bool loadedOnGPU{false};
54-
55-
Buffer vbo{};
56-
Buffer ibo{};
5747

58-
uint32_t vertexCount{0};
59-
uint32_t indexCount{0};
48+
uint64_t get_device_address(VkDevice device);
6049
};
61-
typedef VertexArrays VAO;
6250

6351
} // namespace Graphics
6452

include/engine/graphics/command_buffer.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <engine/graphics/utilities/initializers.h>
1717
#include <engine/graphics/vk_renderpass.h>
1818
#include <engine/graphics/semaphore.h>
19+
#include <engine/graphics/vao.h>
1920

2021
VULKAN_ENGINE_NAMESPACE_BEGIN
2122

@@ -48,7 +49,7 @@ class CommandBuffer
4849
std::vector<Semaphore> waitSemaphores = {},
4950
std::vector<Semaphore> signalSemaphores = {});
5051

51-
VkCommandBuffer get_handle() const {
52+
VkCommandBuffer& get_handle() {
5253
return m_handle;
5354
}
5455

@@ -89,7 +90,7 @@ class CommandPool
8990
CommandPool() {
9091
}
9192

92-
inline VkCommandPool get_handle() const {
93+
inline VkCommandPool& get_handle() {
9394
return m_handle;
9495
}
9596

include/engine/graphics/descriptors.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class DescriptorPool
4848
std::unordered_map<uint32_t, VkDescriptorSetLayout> m_layouts;
4949

5050
public:
51-
inline VkDescriptorPool get_handle() const {
51+
inline VkDescriptorPool& get_handle() {
5252
return m_handle;
5353
}
5454
void init(VkDevice dvc,

include/engine/graphics/device.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <engine/common.h>
1313

14+
#include <engine/graphics/accel.h>
1415
#include <engine/graphics/command_buffer.h>
1516
#include <engine/graphics/extensions.h>
1617
#include <engine/graphics/frame.h>
@@ -57,19 +58,19 @@ class Device
5758
GETTERS
5859
-----------------------------------------------
5960
*/
60-
inline VkDevice get_handle() const {
61+
inline VkDevice& get_handle() {
6162
return m_handle;
6263
};
63-
inline VmaAllocator get_memory_allocator() const {
64+
inline VmaAllocator& get_memory_allocator() {
6465
return m_memory;
6566
}
66-
inline VkPhysicalDevice get_GPU() const {
67+
inline VkPhysicalDevice& get_GPU() {
6768
return m_gpu;
6869
}
6970
inline Swapchain get_swapchain() const {
7071
return m_swapchain;
7172
}
72-
inline std::unordered_map<QueueType, VkQueue> get_queues() const {
73+
inline std::unordered_map<QueueType, VkQueue>& get_queues() {
7374
return m_queues;
7475
}
7576

@@ -124,6 +125,9 @@ class Device
124125
std::vector<Attachment>& attachments,
125126
uint32_t layers = 1);
126127

128+
void create_BLAS(BLAS& accel, VertexArrays& vao);
129+
void create_TLAS(TLAS& accel, std::vector<BLAS>& blases);
130+
127131
/*
128132
DRAWING
129133
-----------------------------------------------

include/engine/graphics/extensions.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,15 @@ DECLARATION OF FUNCTION POINTERS RELATED TO VULKAN EXTENSIONS
2121
#include <engine/common.h>
2222

2323
// Global function pointers
24-
extern PFN_vkCmdSetRasterizationSamplesEXT vkCmdSetRasterizationSamples;
25-
extern PFN_vkCmdSetPolygonModeEXT vkCmdSetPolygonMode;
26-
27-
void load_EXT_functions(VkDevice& device);
24+
extern PFN_vkCmdSetRasterizationSamplesEXT vkCmdSetRasterizationSamples;
25+
extern PFN_vkCmdSetPolygonModeEXT vkCmdSetPolygonMode;
26+
extern PFN_vkCreateAccelerationStructureKHR vkCreateAccelerationStructure;
27+
extern PFN_vkDestroyAccelerationStructureKHR vkDestroyAccelerationStructure;
28+
extern PFN_vkGetAccelerationStructureBuildSizesKHR vkGetAccelerationStructureBuildSizes;
29+
extern PFN_vkGetAccelerationStructureDeviceAddressKHR vkGetAccelerationStructureDeviceAddress;
30+
extern PFN_vkCmdBuildAccelerationStructuresKHR vkCmdBuildAccelerationStructures;
31+
extern PFN_vkBuildAccelerationStructuresKHR vkBuildAccelerationStructures;
32+
33+
void load_extensions(VkDevice& device);
2834

2935
#endif

include/engine/graphics/framebuffer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Framebuffer
2727
Framebuffer() {
2828
}
2929

30-
inline VkFramebuffer get_handle() const {
30+
inline VkFramebuffer& get_handle() {
3131
return m_handle;
3232
}
3333

0 commit comments

Comments
 (0)