~ | BVH sent to gpu

This commit is contained in:
TheRedShip
2025-01-17 16:27:39 +01:00
parent 6f80a66550
commit 6fdea11adb
14 changed files with 359 additions and 46 deletions

View File

@ -20,7 +20,8 @@ int main(int argc, char **argv)
return (1);
Window window(&scene, WIDTH, HEIGHT, "RT_GPU", 0);
Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/compute.glsl");
// Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/compute.glsl");
Shader shader("shaders/vertex.vert", "shaders/frag.frag", "shaders/debug.glsl");
GLint max_gpu_size;
glGetIntegerv(GL_MAX_SHADER_STORAGE_BLOCK_SIZE, &max_gpu_size);
@ -51,6 +52,12 @@ int main(int argc, char **argv)
glBufferData(GL_SHADER_STORAGE_BUFFER, scene.getGPULights().size() * sizeof(int), nullptr, GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 3, lightSSBO);
GLuint bvhSSBO;
glGenBuffers(1, &bvhSSBO);
glBindBuffer(GL_SHADER_STORAGE_BUFFER, bvhSSBO);
glBufferData(GL_SHADER_STORAGE_BUFFER, scene.getBVH().size() * sizeof(GPUBvh), nullptr, GL_STATIC_DRAW);
glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 4, bvhSSBO);
GLuint cameraUBO;
glGenBuffers(1, &cameraUBO);
@ -86,6 +93,11 @@ int main(int argc, char **argv)
glBindBuffer(GL_SHADER_STORAGE_BUFFER, lightSSBO);
glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, gpu_lights_array.size() * sizeof(int), gpu_lights_array.data());
std::vector<GPUBvh> gpu_bvh = scene.getBVH();
glBindBuffer(GL_SHADER_STORAGE_BUFFER, bvhSSBO);
glBufferSubData(GL_SHADER_STORAGE_BUFFER, 0, gpu_bvh.size() * sizeof(GPUBvh), gpu_bvh.data());
GPUCamera camera_data = scene.getCamera()->getGPUData();
glBindBuffer(GL_UNIFORM_BUFFER, cameraUBO);
glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(GPUCamera), &camera_data);
@ -96,6 +108,7 @@ int main(int argc, char **argv)
shader.set_int("u_frameCount", window.getFrameCount());
shader.set_int("u_objectsNum", object_data.size());
shader.set_int("u_bvhNum", gpu_bvh.size());
shader.set_int("u_lightsNum", gpu_lights.size());
shader.set_int("u_pixelisation", window.getPixelisation());
shader.set_float("u_time", (float)(glfwGetTime()));

View File

@ -34,12 +34,6 @@ void BVH::updateBounds(std::vector <GPUObject> primitives)
if (leaf_triangle.type != (int)Object::Type::TRIANGLE)
continue ;
if (leaf_triangle.type == (int)Object::Type::TRIANGLE)
{
leaf_triangle.vertex1 += leaf_triangle.position;
leaf_triangle.vertex2 += leaf_triangle.position;
}
_aabb.min = glm::min(_aabb.min, leaf_triangle.position);
_aabb.min = glm::min(_aabb.min, leaf_triangle.vertex1);
@ -63,7 +57,7 @@ void BVH::subdivide(std::vector<GPUObject> primitives)
if (extent.y > extent.x) axis = 1;
if (extent.z > extent[axis]) axis = 2;
glm::vec3 split_pos = _aabb.min + extent[axis] * 0.5f;
float split_pos = _aabb.min[axis] + extent[axis] * 0.5f;
int i = _first_primitive;
int j = _first_primitive + _primitive_count - 1;
@ -73,7 +67,7 @@ void BVH::subdivide(std::vector<GPUObject> primitives)
glm::vec3 centroid = primitives[i].position + primitives[i].vertex1 + primitives[i].vertex2;
centroid /= 3.0f;
if (centroid[axis] < split_pos[axis])
if (centroid[axis] < split_pos)
i++;
else
{
@ -103,7 +97,6 @@ void BVH::showAABB(Scene *scene)
{
if (_is_leaf)
{
scene->addObject(new Sphere(_aabb.min, 0.5f, 6));
scene->addObject(new Sphere(_aabb.max, 0.5f, 6));
scene->addObject(new Sphere(glm::vec3(_aabb.min.x, _aabb.min.y, _aabb.max.z), 0.5f, 6));
@ -112,7 +105,6 @@ void BVH::showAABB(Scene *scene)
scene->addObject(new Sphere(glm::vec3(_aabb.min.x, _aabb.max.y, _aabb.max.z), 0.5f, 6));
scene->addObject(new Sphere(glm::vec3(_aabb.max.x, _aabb.min.y, _aabb.max.z), 0.5f, 6));
scene->addObject(new Sphere(glm::vec3(_aabb.max.x, _aabb.max.y, _aabb.min.z), 0.5f, 6));
}
else
{
@ -124,4 +116,60 @@ void BVH::showAABB(Scene *scene)
const AABB &BVH::getAABB() const
{
return (_aabb);
}
GPUBvh BVH::toGPUBvh()
{
GPUBvh bvh;
bvh.is_leaf = _is_leaf;
bvh.first_primitive = _first_primitive;
bvh.primitive_count = _primitive_count;
bvh.max = _aabb.max;
bvh.min = _aabb.min;
return (bvh);
}
void BVH::flatten(std::vector<GPUBvh> &bvhs, int &currentIndex)
{
GPUBvh self_bvh = toGPUBvh();
int self_index = currentIndex++;
self_bvh.left_index = -1;
self_bvh.right_index = -1;
if (!_is_leaf)
{
self_bvh.left_index = currentIndex;
_left->flatten(bvhs, currentIndex);
self_bvh.right_index = currentIndex;
_right->flatten(bvhs, currentIndex);
}
bvhs[self_index] = self_bvh;
}
int BVH::size()
{
int count = 0;
if (_is_leaf)
return (0);
count += 1 + _left->size();
count += 1 + _right->size();
return (count);
}
std::vector<GPUBvh> BVH::getGPUBvhs()
{
std::vector<GPUBvh> bvhs(size() + 1);
int currentIndex = 0;
flatten(bvhs, currentIndex);
return (bvhs);
}

View File

@ -53,11 +53,11 @@ bool Scene::parseScene(char *name)
}
file.close();
//bvh
BVH *bvh = new BVH(_gpu_objects, 0, _gpu_objects.size());
bvh->showAABB(this);
// addObject(new Cube((bvh->getAABB().max + bvh->getAABB().min) / 2.0f, bvh->getAABB().max - bvh->getAABB().min, 7));
//
BVH *bvh = new BVH(_gpu_objects, 0, _gpu_objects.size());
_gpu_bvh = bvh->getGPUBvhs();
// bvh->showAABB(this);
return (true);
}
@ -162,27 +162,32 @@ void Scene::updateLightAndObjects(int mat_id)
}
}
std::set<int> Scene::getGPULights()
std::set<int> Scene::getGPULights()
{
return (_gpu_lights);
}
const std::vector<GPUObject>& Scene::getObjectData() const
const std::vector<GPUObject> &Scene::getObjectData() const
{
return (_gpu_objects);
}
std::vector<GPUMaterial>& Scene::getMaterialData()
std::vector<GPUMaterial> &Scene::getMaterialData()
{
return (_gpu_materials);
}
GPUVolume &Scene::getVolume()
GPUVolume &Scene::getVolume()
{
return (_gpu_volume);
}
Camera *Scene::getCamera(void) const
std::vector<GPUBvh> &Scene::getBVH()
{
return (_gpu_bvh);
}
Camera *Scene::getCamera(void) const
{
return (_camera);
}
@ -192,4 +197,4 @@ GPUMaterial Scene::getMaterial(int material_index)
if (material_index < 0 || material_index >= (int)_gpu_materials.size())
throw std::runtime_error("Incorrect material index");
return (_gpu_materials[material_index]);
}
}