mirror of
https://github.com/TheRedShip/RT_GPU.git
synced 2025-09-27 18:48:36 +02:00
~ | BVH optimizing again
This commit is contained in:
@ -20,8 +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/debug.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);
|
||||
@ -128,7 +128,7 @@ int main(int argc, char **argv)
|
||||
|
||||
recorded_fps.push_back((int)window.getFps());
|
||||
|
||||
float y_offset = 0.;
|
||||
float y_offset = 0;
|
||||
float dist_to_obj = 2;
|
||||
float speed = 0.5;
|
||||
|
||||
|
@ -145,26 +145,74 @@ void BVH::flatten(std::vector<GPUBvh> &bvhs, int ¤tIndex)
|
||||
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);
|
||||
std::vector<GPUBvh> bvhs(getSize() + 1);
|
||||
|
||||
int currentIndex = 0;
|
||||
flatten(bvhs, currentIndex);
|
||||
|
||||
|
||||
return (bvhs);
|
||||
}
|
||||
|
||||
int BVH::getSize()
|
||||
{
|
||||
int count = 0;
|
||||
|
||||
if (_is_leaf)
|
||||
return (0);
|
||||
|
||||
count += 1 + _left->getSize();
|
||||
count += 1 + _right->getSize();
|
||||
|
||||
return (count);
|
||||
}
|
||||
|
||||
int BVH::getLeaves()
|
||||
{
|
||||
int count = 0;
|
||||
|
||||
if (_is_leaf)
|
||||
return (1);
|
||||
|
||||
count += _left->getLeaves();
|
||||
count += _right->getLeaves();
|
||||
|
||||
return (count);
|
||||
}
|
||||
|
||||
//get tri per leaf min max and average
|
||||
BVHStats BVH::analyzeBVHLeaves(BVH *root)
|
||||
{
|
||||
if (!root)
|
||||
return {0, 0, 0.0f};
|
||||
// If this is a leaf node, return its stats
|
||||
|
||||
if (root->_is_leaf)
|
||||
return {
|
||||
root->_primitive_count, // min
|
||||
root->_primitive_count, // max
|
||||
(float)root->_primitive_count // average
|
||||
};
|
||||
|
||||
// Get stats from left and right subtrees
|
||||
BVHStats left = analyzeBVHLeaves(root->_left);
|
||||
BVHStats right = analyzeBVHLeaves(root->_right);
|
||||
|
||||
// Combine the results
|
||||
int min_count = std::min(left.min_triangles, right.min_triangles);
|
||||
int max_count = std::max(left.max_triangles, right.max_triangles);
|
||||
|
||||
// Calculate weighted average based on number of leaves in each subtree
|
||||
float left_leaf_count = (left.average_triangles > 0) ? 1.0f : 0.0f;
|
||||
float right_leaf_count = (right.average_triangles > 0) ? 1.0f : 0.0f;
|
||||
float total_leaf_count = left_leaf_count + right_leaf_count;
|
||||
|
||||
float avg_count = 0.0f;
|
||||
if (total_leaf_count > 0)
|
||||
avg_count = (left.average_triangles * left_leaf_count +
|
||||
right.average_triangles * right_leaf_count) / total_leaf_count;
|
||||
|
||||
return {min_count, max_count, avg_count};
|
||||
}
|
@ -64,8 +64,15 @@ bool Scene::parseScene(char *name)
|
||||
BVH *bvh = new BVH(_gpu_triangles, 0, _gpu_triangles.size());
|
||||
_gpu_bvh = bvh->getGPUBvhs();
|
||||
|
||||
std::cout << "BVH Done: " << bvh->size() << std::endl;
|
||||
std::cout << "BVH Done: " << std::endl;
|
||||
|
||||
std::cout << "\tBVH size: " << bvh->getSize() << std::endl;
|
||||
std::cout << "\tBVH leaves: " << bvh->getLeaves() << std::endl << std::endl;
|
||||
|
||||
BVHStats stats = bvh->analyzeBVHLeaves(bvh);
|
||||
std::cout << "\tMin triangles per leaf: " << stats.min_triangles << std::endl;
|
||||
std::cout << "\tMax triangles per leaf: " << stats.max_triangles << std::endl;
|
||||
std::cout << "\tAverage triangles per leaf: " << stats.average_triangles << std::endl << std::endl;
|
||||
|
||||
return (true);
|
||||
}
|
||||
|
@ -248,7 +248,7 @@ void Window::imGuiRender()
|
||||
|
||||
ImGui::End();
|
||||
|
||||
ImGui::Begin("Debug");
|
||||
ImGui::Begin("Debug BVH");
|
||||
|
||||
has_changed |= ImGui::Checkbox("Enable", (bool *)(&_scene->getDebug().enabled));
|
||||
ImGui::Separator();
|
||||
|
Reference in New Issue
Block a user