~ | TopLevelBVH

This commit is contained in:
RedShip
2025-01-22 19:33:18 +01:00
parent 594903029f
commit cd65cff03f
10 changed files with 305 additions and 115 deletions

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/27 14:51:49 by TheRed #+# #+# */
/* Updated: 2025/01/20 18:55:25 by ycontre ### ########.fr */
/* Updated: 2025/01/22 18:45:48 by ycontre ### ########.fr */
/* */
/* ************************************************************************** */
@ -30,6 +30,7 @@ int main(int argc, char **argv)
const std::vector<GPUTriangle> &triangle_data = scene.getTriangleData();
const std::vector<GPUBvh> &bvh_nodes = scene.getBvh();
const std::vector<GPUBvhData> &bvh_data = scene.getBvhData();
const std::vector<GPUTopBvh> &top_bvh = scene.getTopBvh();
const std::vector<GPUMaterial> &material_data = scene.getMaterialData();
std::cout << "Sending " << object_data.size() << " objects for " << \
@ -51,29 +52,35 @@ int main(int argc, char **argv)
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GPUTriangle) * triangle_data.size(), triangle_data.data(), GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 2, trianglesSSBO);
GLuint bvh_nodesSSBO;
glGenBuffers(1, &bvh_nodesSSBO);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, bvh_nodesSSBO);
GLuint top_bvhSSBO;
glGenBuffers(1, &top_bvhSSBO);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, top_bvhSSBO);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GPUTopBvh) * top_bvh.size(), top_bvh.data(), GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, top_bvhSSBO);
GLuint bvh_dataSSBO;
glGenBuffers(1, &bvh_dataSSBO);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, bvh_dataSSBO);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GPUBvhData) * bvh_data.size(), bvh_data.data(), GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, bvh_nodesSSBO);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, bvh_dataSSBO);
GLuint bvhSSBO;
glGenBuffers(1, &bvhSSBO);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, bvhSSBO);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GPUBvh) * bvh_nodes.size(), bvh_nodes.data(), GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, bvhSSBO);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 5, bvhSSBO);
GLuint materialSSBO;
glGenBuffers(1, &materialSSBO);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, materialSSBO);
glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(GPUMaterial) * material_data.size(), nullptr, GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 5, materialSSBO);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 6, materialSSBO);
GLuint lightSSBO;
glGenBuffers(1, &lightSSBO);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, lightSSBO);
glBufferData(GL_SHADER_STORAGE_BUFFER, scene.getGPULights().size() * sizeof(int), nullptr, GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 6, lightSSBO);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 7, lightSSBO);
GLuint cameraUBO;

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/16 21:48:48 by TheRed #+# #+# */
/* Updated: 2025/01/19 18:30:27 by ycontre ### ########.fr */
/* Updated: 2025/01/22 19:23:38 by ycontre ### ########.fr */
/* */
/* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/23 18:29:41 by ycontre #+# #+# */
/* Updated: 2025/01/21 14:45:04 by tomoron ### ########.fr */
/* Updated: 2025/01/22 19:17:21 by ycontre ### ########.fr */
/* */
/* ************************************************************************** */
@ -59,7 +59,7 @@ bool Scene::parseScene(char *name)
file.close();
TopBVH *top_bvh = new TopBVH(_gpu_bvh_data, _gpu_bvh, 0, _gpu_bvh_data.size());
(void) top_bvh;
_gpu_top_bvh = top_bvh->getGPUTopBvhs();
std::cout << "Parsing done" << std::endl;
@ -252,7 +252,12 @@ GPUDebug &Scene::getDebug()
return (_gpu_debug);
}
std::vector<GPUBvhData> &Scene::getBvhData()
std::vector<GPUTopBvh> &Scene::getTopBvh()
{
return (_gpu_top_bvh);
}
std::vector<GPUBvhData> &Scene::getBvhData()
{
return (_gpu_bvh_data);
}

View File

@ -6,7 +6,7 @@
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/26 21:43:51 by TheRed #+# #+# */
/* Updated: 2025/01/21 15:57:42 by tomoron ### ########.fr */
/* Updated: 2025/01/22 17:50:36 by ycontre ### ########.fr */
/* */
/* ************************************************************************** */

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* TopBvh.cpp :+: :+: :+: */
/* TopBVH.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: TheRed <TheRed@students.42.fr> +#+ +:+ +#+ */
/* By: ycontre <ycontre@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/21 21:46:45 by TheRed #+# #+# */
/* Updated: 2025/01/21 21:46:45 by TheRed ### ########.fr */
/* Updated: 2025/01/22 19:32:43 by ycontre ### ########.fr */
/* */
/* ************************************************************************** */
@ -54,8 +54,8 @@ float TopBVH::evaluateSah(std::vector<GPUBvhData> &bvhs_data, std::vector<GPUBvh
GPUBvhData bvh_data = bvhs_data[_first_bvh + i];
GPUBvh bvh_root = bvhs[bvh_data.bvh_start_index];
glm::vec3 min = bvh_root.min - bvh_data.offset;
glm::vec3 max = bvh_root.max - bvh_data.offset;
glm::vec3 min = bvh_root.min + bvh_data.offset;
glm::vec3 max = bvh_root.max + bvh_data.offset;
if (min[axis] < pos)
{
@ -117,8 +117,8 @@ void TopBVH::subdivide(std::vector<GPUBvhData> &bvhs_data, std::vector<GPUBvh> &
{
GPUBvhData bvh_data = bvhs_data[i];
GPUBvh root = bvhs[bvh_data.bvh_start_index];
glm::vec3 min = root.min - bvh_data.offset;
glm::vec3 max = root.max - bvh_data.offset;
glm::vec3 min = root.min + bvh_data.offset;
glm::vec3 max = root.max + bvh_data.offset;
glm::vec3 centroid = (min + max) * 0.5f;
if (centroid[axis] < split_pos)
@ -141,4 +141,54 @@ void TopBVH::subdivide(std::vector<GPUBvhData> &bvhs_data, std::vector<GPUBvh> &
_right = new TopBVH(bvhs_data, bvhs, i, _bvh_count - left_count);
_bvh_count = 0;
}
int TopBVH::getSize()
{
int count = 0;
if (_is_leaf)
return (0);
count += 1 + _left->getSize();
count += 1 + _right->getSize();
return (count);
}
void TopBVH::flatten(std::vector<GPUTopBvh> &top_bvhs, int &currentIndex)
{
GPUTopBvh self_top_bvh;
self_top_bvh.is_leaf = _is_leaf;
self_top_bvh.first_bvh_data = _first_bvh;
self_top_bvh.bvh_data_count = _bvh_count;
self_top_bvh.max = _aabb.max;
self_top_bvh.min = _aabb.min;
int self_index = currentIndex++;
self_top_bvh.left_index = -1;
self_top_bvh.right_index = -1;
if (!_is_leaf)
{
self_top_bvh.left_index = currentIndex;
_left->flatten(top_bvhs, currentIndex);
self_top_bvh.right_index = currentIndex;
_right->flatten(top_bvhs, currentIndex);
}
top_bvhs[self_index] = self_top_bvh;
}
std::vector<GPUTopBvh> TopBVH::getGPUTopBvhs()
{
std::vector<GPUTopBvh> bvhs(getSize() + 1);
int currentIndex = 0;
flatten(bvhs, currentIndex);
return (bvhs);
}