aboutsummaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authorladevieq <ladevie.quentin@gmail.com>2021-07-25 11:58:01 +0200
committerladevieq <ladevie.quentin@gmail.com>2021-07-25 11:58:01 +0200
commit3dd870b001bef62aaa501257352d968d1346c7f2 (patch)
treee82abc32ace01af978071b32db40e5c07c991529 /include
parent6bdf3ab65a7c1e98595ad167479b94a82184a444 (diff)
Use a different buffer for scene info, geometry, bvh and load gltf
Diffstat (limited to 'include')
-rw-r--r--include/aabb.hpp9
-rw-r--r--include/bvh-node.hpp74
-rw-r--r--include/gltf.hpp34
-rw-r--r--include/scene.hpp45
-rw-r--r--include/triangle.hpp25
-rw-r--r--include/utils.hpp1
-rw-r--r--include/vk-renderer.hpp12
7 files changed, 163 insertions, 37 deletions
diff --git a/include/aabb.hpp b/include/aabb.hpp
index 76a6872..2630a17 100644
--- a/include/aabb.hpp
+++ b/include/aabb.hpp
@@ -10,6 +10,15 @@ class aabb {
aabb() = default;
aabb(vec3 min_point, vec3 max_point): minimum(min_point), maximum(max_point) {}
aabb(vec3 position, float radius): minimum(position - vec3(radius)), maximum(position + vec3(radius)) {}
+ aabb(point3 v1, point3 v2, point3 v3) {
+ minimum.x = fmin(fmin(v1.x, v2.x), v3.x);
+ minimum.y = fmin(fmin(v1.y, v2.y), v3.y);
+ minimum.z = fmin(fmin(v1.z, v2.z), v3.z);
+
+ maximum.x = fmax(fmax(v1.x, v2.x), v3.x);
+ maximum.y = fmax(fmax(v1.y, v2.y), v3.y);
+ maximum.z = fmax(fmax(v1.z, v2.z), v3.z);
+ }
static aabb surrounding_box(aabb box0, aabb box1) {
point3 min_point(fmin(box0.minimum.x, box1.minimum.x),
diff --git a/include/bvh-node.hpp b/include/bvh-node.hpp
index 16c3cb3..805edf2 100644
--- a/include/bvh-node.hpp
+++ b/include/bvh-node.hpp
@@ -6,16 +6,17 @@
#include "aabb.hpp"
#include "sphere.hpp"
+#include "triangle.hpp"
class bvh_node {
public:
bvh_node() = default;
- bvh_node(sphere& sphere, int32_t sphere_id): bounding_box(sphere.bounding_box), sphere_id(sphere_id){}
+ bvh_node(sphere& sphere, int32_t sphere_id): bounding_box(sphere.bounding_box), primitive_id(sphere_id){}
aabb bounding_box;
int32_t next_id = -1;
- int32_t sphere_id = -1;
+ int32_t primitive_id = -1;
uint32_t padding[2];
};
@@ -26,21 +27,21 @@ struct temp_node : public bvh_node {
class bvh {
public:
- void build(sphere* spheres, uint32_t spheres_count) {
- nodes = std::vector<bvh_node>(2 * spheres_count - 1);
- temp_nodes = std::vector<temp_node>(2 * spheres_count - 1);
- leafs = std::vector<temp_node>(spheres_count);
- this->spheres = spheres;
+ bvh(std::vector<sphere>& spheres, std::vector<bvh_node>& nodes)
+ :spheres(spheres), nodes(nodes) {
+ nodes = std::vector<bvh_node>(2 * spheres.size() - 1);
+ temp_nodes = std::vector<temp_node>(2 * spheres.size() - 1);
+ leafs = std::vector<temp_node>(spheres.size());
- for (uint32_t id = 0; id < spheres_count; id++) {
+ for (uint32_t id = 0; id < spheres.size(); id++) {
leafs[id].bounding_box = spheres[id].bounding_box;
- leafs[id].sphere_id = id;
+ leafs[id].primitive_id = id;
}
temp_nodes[0] = temp_node();
- temp_nodes[0].bounding_box = compute_bounds(0, spheres_count);
- subdivide(0, 0, spheres_count);
+ temp_nodes[0].bounding_box = compute_bounds(0, spheres.size());
+ subdivide(0, 0, spheres.size());
set_depth_first_order();
@@ -48,7 +49,7 @@ public:
auto old_node = temp_nodes[id];
auto &node = nodes[old_node.df_id];
- node.sphere_id = old_node.sphere_id;
+ node.primitive_id = old_node.primitive_id;
node.bounding_box = old_node.bounding_box;
if (old_node.next_id != -1) {
@@ -59,6 +60,39 @@ public:
// exporter();
}
+ // bvh(std::vector<triangle>& triangles, std::vector<bvh_node>& nodes)
+ // :triangles(triangles), nodes(nodes) {
+ // nodes.resize(2 * triangles.size() - 1);
+ // temp_nodes = std::vector<temp_node>(2 * triangles.size() - 1);
+ // leafs = std::vector<temp_node>(triangles.size());
+
+ // for (uint32_t id = 0; id < triangles.size(); id++) {
+ // leafs[id].bounding_box = triangles[id].bounding_box;
+ // leafs[id].primitive_id = id;
+ // }
+
+ // temp_nodes[0] = temp_node();
+
+ // temp_nodes[0].bounding_box = compute_bounds(0, triangles.size());
+ // subdivide(0, 0, triangles.size());
+
+ // set_depth_first_order();
+
+ // for (uint32_t id = 0; id < temp_nodes.size(); id++) {
+ // auto old_node = temp_nodes[id];
+ // auto &node = nodes[old_node.df_id];
+
+ // node.primitive_id = old_node.primitive_id;
+ // node.bounding_box = old_node.bounding_box;
+
+ // if (old_node.next_id != -1) {
+ // node.next_id = temp_nodes[old_node.next_id].df_id;
+ // }
+ // }
+
+ // // exporter();
+ // }
+
//-------------------------
// BVH exporter for graphviz
//-------------------------
@@ -92,7 +126,7 @@ public:
return -1;
}
- if (nodes[id].sphere_id == -1) {
+ if (nodes[id].primitive_id == -1) {
int32_t left = id + 1;
int32_t next_id;
@@ -102,7 +136,7 @@ public:
if (next_id != -1) {
return traverse_depth_first_ordered(nodes, id, next_id);
}
- } else if (nodes[id].sphere_id != -1 && nodes[id].next_id != -1) {
+ } else if (nodes[id].primitive_id != -1 && nodes[id].next_id != -1) {
std::cout << "\t " << parent_id << " -> " << id << ";\n";
return nodes[id].next_id;
} else {
@@ -119,7 +153,8 @@ private:
for (uint32_t box_id = begin; box_id < end; box_id++) {
auto leaf = leafs[box_id];
- global_box = aabb::surrounding_box(global_box, spheres[leaf.sphere_id].bounding_box);
+ global_box = aabb::surrounding_box(global_box, spheres[leaf.primitive_id].bounding_box);
+ // global_box = aabb::surrounding_box(global_box, triangles[leaf.primitive_id].bounding_box);
}
return global_box;
@@ -161,7 +196,7 @@ private:
std::sort(leafs.begin() + begin, leafs.begin() + end,
[&](const bvh_node& a, const bvh_node& b) {
- return spheres[a.sphere_id].center[split_axis] < spheres[b.sphere_id].center[split_axis];
+ return spheres[a.primitive_id].center[split_axis] < spheres[b.primitive_id].center[split_axis];
}
);
@@ -191,10 +226,13 @@ private:
}
public:
- std::vector<bvh_node> nodes;
+ std::vector<bvh_node>& nodes;
std::vector<temp_node> temp_nodes;
std::vector<temp_node> leafs;
- sphere* spheres;
+
+ std::vector<sphere>& spheres;
+ // std::vector<triangle>& triangles;
+
uint32_t next_node_id = 0;
};
diff --git a/include/gltf.hpp b/include/gltf.hpp
new file mode 100644
index 0000000..80f2749
--- /dev/null
+++ b/include/gltf.hpp
@@ -0,0 +1,34 @@
+#ifndef __GLTF_HPP_
+#define __GLTF_HPP_
+
+#include <filesystem>
+#include <vector>
+
+#include <nlohmann/json.hpp>
+
+using json = nlohmann::json;
+
+#include <vec3.hpp>
+
+struct mesh_part {
+ std::vector<uint16_t> indices;
+ std::vector<vec3> positions;
+ std::vector<vec3> normals;
+};
+
+struct mesh {
+ std::vector<std::vector<mesh_part>> nodes;
+};
+
+class gltf {
+public:
+ static mesh load(std::filesystem::path &path, std::string &filename);
+
+private:
+
+ static void load_node(json &gltf_json, json &node, std::vector<std::vector<uint8_t>> &buffers_content, mesh& gltf_mesh);
+
+ static void load_primitive(json &gltf_json, json &primitive, std::vector<std::vector<uint8_t>> &buffers_content, mesh& gltf_mesh);
+};
+
+#endif
diff --git a/include/scene.hpp b/include/scene.hpp
index b7e1100..364e3a7 100644
--- a/include/scene.hpp
+++ b/include/scene.hpp
@@ -3,39 +3,48 @@
#include "vec3.hpp"
#include "sphere.hpp"
+#include "triangle.hpp"
#include "camera.hpp"
#include "bvh-node.hpp"
class scene {
-public:
- scene(uint32_t width, uint32_t height);
+ struct metadata {
+ color sky_color = { 0.5, 0.7, 1.0 };
+ color ground_color = { 1.0, 1.0, 1.0 };
+
+ camera cam;
- color sky_color = { 0.5, 0.7, 1.0 };
- color ground_color = { 1.0, 1.0, 1.0 };
+ uint32_t max_bounce = 50;
- camera cam;
+ // Output image resolution
+ uint32_t width;
+ uint32_t height;
- sphere spheres[512] = {};
- bvh_node nodes[1024] = {};
+ uint32_t sample_index = 0;
- uint32_t max_bounce = 50;
+ uint32_t spheres_count = 0;
+ // uint32_t triangles_count = 0;
+ uint32_t enable_dof = true;
+ uint32_t debug_bvh = false;
+ uint32_t padding;
- // Output image resolution
- uint32_t width;
- uint32_t height;
+ metadata(camera cam, uint32_t width, uint32_t height)
+ : cam(cam), width(width), height(height) {}
+ };
- uint32_t sample_index = 0;
+ void random_scene();
- uint32_t spheres_count = 0;
- uint32_t enable_dof = true;
- uint32_t debug_bvh = false;
- uint32_t padding;
+public:
+ scene(camera cam, uint32_t width, uint32_t height);
-private:
+ metadata meta;
- uint32_t random_scene(sphere* world);
+ // Geometry & BVH
+ std::vector<sphere> spheres;
+ // std::vector<triangle> triangles;
+ std::vector<bvh_node> nodes;
};
#endif // !__SCENE_HPP_
diff --git a/include/triangle.hpp b/include/triangle.hpp
new file mode 100644
index 0000000..12c0b69
--- /dev/null
+++ b/include/triangle.hpp
@@ -0,0 +1,25 @@
+#ifndef __TRIANGLE_HPP_
+#define __TRIANGLE_HPP_
+
+#include "aabb.hpp"
+#include "material.hpp"
+
+struct vertex {
+ vec3 position;
+ vec3 normal;
+};
+
+struct triangle {
+ triangle() = default;
+ triangle(vertex v1, vertex v2, vertex v3)
+ : v1(v1), v2(v2), v3(v3), bounding_box(v1.position, v2.position, v3.position)
+ {}
+
+ vertex v1;
+ vertex v2;
+ vertex v3;
+
+ aabb bounding_box;
+};
+
+#endif // !__TRIANGLE_HPP_
diff --git a/include/utils.hpp b/include/utils.hpp
index b00db81..e7178d8 100644
--- a/include/utils.hpp
+++ b/include/utils.hpp
@@ -2,6 +2,7 @@
#define __UTILS_HPP_
#include <string>
+#include <filesystem>
#include <vector>
std::vector<uint8_t> read_file(const char* path);
diff --git a/include/vk-renderer.hpp b/include/vk-renderer.hpp
index b478b25..775ea89 100644
--- a/include/vk-renderer.hpp
+++ b/include/vk-renderer.hpp
@@ -11,7 +11,7 @@ class window;
class vkrenderer {
public:
- vkrenderer(window& wnd, size_t scene_buffer_size);
+ vkrenderer(window& wnd, size_t scene_buffer_size, size_t geometry_buffer_size, size_t bvh_buffer_size);
~vkrenderer();
@@ -31,6 +31,14 @@ class vkrenderer {
return scene_buffer.alloc_info.pMappedData;
}
+ inline void* geometry_buffer_ptr() {
+ return geometry_buffer.alloc_info.pMappedData;
+ }
+
+ inline void* bvh_buffer_ptr() {
+ return bvh_buffer.alloc_info.pMappedData;
+ }
+
private:
void handle_swapchain_result(VkResult function_result);
@@ -52,6 +60,8 @@ class vkrenderer {
Pipeline compute_pipeline;
Buffer scene_buffer;
+ Buffer geometry_buffer;
+ Buffer bvh_buffer;
std::vector<VkDescriptorSetLayoutBinding> tonemapping_sets_bindings;