Skip to content
Snippets Groups Projects
Verified Commit 5a0812f7 authored by Tobias Frisch's avatar Tobias Frisch
Browse files

[#56] Fixed/Completed frustum culling the 2nd

parent 16ba7a91
No related branches found
No related tags found
1 merge request!45Resolve "Szene-Repräsentation"
Pipeline #26233 failed
......@@ -5,6 +5,7 @@
namespace vkcv {
enum class LogLevel {
RAW_INFO,
INFO,
WARNING,
ERROR
......@@ -12,6 +13,7 @@ namespace vkcv {
constexpr auto getLogOutput(LogLevel level) {
switch (level) {
case LogLevel::RAW_INFO:
case LogLevel::INFO:
return stdout;
default:
......@@ -21,6 +23,7 @@ namespace vkcv {
constexpr const char* getLogName(LogLevel level) {
switch (level) {
case LogLevel::RAW_INFO:
case LogLevel::INFO:
return "INFO";
case LogLevel::WARNING:
......@@ -41,24 +44,33 @@ namespace vkcv {
#define __PRETTY_FUNCTION__ __FUNCSIG__
#endif
#define vkcv_log(level, ...) { \
char output_message [ \
VKCV_DEBUG_MESSAGE_LEN \
]; \
snprintf( \
output_message, \
VKCV_DEBUG_MESSAGE_LEN, \
__VA_ARGS__ \
); \
fprintf( \
getLogOutput(level), \
"[%s]: %s [%s, line %d: %s]\n", \
vkcv::getLogName(level), \
output_message, \
__FILE__, \
__LINE__, \
__PRETTY_FUNCTION__ \
); \
#define vkcv_log(level, ...) { \
char output_message [ \
VKCV_DEBUG_MESSAGE_LEN \
]; \
snprintf( \
output_message, \
VKCV_DEBUG_MESSAGE_LEN, \
__VA_ARGS__ \
); \
if (level != LogLevel::RAW_INFO) { \
fprintf( \
getLogOutput(level), \
"[%s]: %s [%s, line %d: %s]\n", \
vkcv::getLogName(level), \
output_message, \
__FILE__, \
__LINE__, \
__PRETTY_FUNCTION__ \
); \
} else { \
fprintf( \
getLogOutput(level), \
"[%s]: %s\n", \
vkcv::getLogName(level), \
output_message \
); \
} \
}
#else
......
......@@ -27,6 +27,9 @@ namespace vkcv::scene {
void recordDrawcalls(const glm::mat4& viewProjection,
std::vector<glm::mat4>& matrices,
std::vector<DrawcallInfo>& drawcalls);
[[nodiscard]]
size_t getDrawcallCount() const;
public:
~Mesh();
......
......@@ -26,6 +26,9 @@ namespace vkcv::scene {
void recordDrawcalls(const glm::mat4& viewProjection,
std::vector<glm::mat4>& matrices,
std::vector<DrawcallInfo>& drawcalls);
[[nodiscard]]
size_t getDrawcallCount() const;
public:
~Node();
......
......@@ -79,10 +79,21 @@ namespace vkcv::scene {
return *this;
}
static glm::vec3 projectPoint(const glm::mat4& transform, const glm::vec3& point) {
static glm::vec3 projectPoint(const glm::mat4& transform, const glm::vec3& point, bool& negative_w) {
const glm::vec4 position = transform * glm::vec4(point, 1.0f);
const float perspective = std::abs(position[3]);
/*
* We divide by the absolute of the 4th coorditnate because
* clipping is weird and points have to move to the other
* side of the camera.
*
* We also need to collect if the 4th coordinate was negative
* to know if all corners are behind the camera. So these can
* be culled as well
*/
negative_w = (position[3] < 0.0f);
return glm::vec3(
position[0] / perspective,
position[1] / perspective,
......@@ -109,16 +120,19 @@ namespace vkcv::scene {
const Bounds& bounds = part.getBounds();
const auto corners = bounds.getCorners();
auto projected = projectPoint(transform, corners[0]);
bool negative_w;
auto projected = projectPoint(transform, corners[0], negative_w);
Bounds aabb (projected, projected);
for (size_t j = 1; j < corners.size(); j++) {
projected = projectPoint(transform, corners[j]);
bool flag_w;
projected = projectPoint(transform, corners[j], flag_w);
aabb.extend(projected);
negative_w &= flag_w;
}
if (!checkFrustum(aabb)) {
if ((negative_w) || (!checkFrustum(aabb))) {
continue;
}
......@@ -126,5 +140,9 @@ namespace vkcv::scene {
drawcalls.push_back(m_drawcalls[i]);
}
}
size_t Mesh::getDrawcallCount() const {
return m_drawcalls.size();
}
}
......@@ -66,5 +66,19 @@ namespace vkcv::scene {
node.recordDrawcalls(viewProjection, matrices, drawcalls);
}
}
size_t Node::getDrawcallCount() const {
size_t count = 0;
for (auto& mesh : m_meshes) {
count += mesh.getDrawcallCount();
}
for (auto& node : m_nodes) {
count += node.getDrawcallCount();
}
return count;
}
}
......@@ -73,13 +73,17 @@ namespace vkcv::scene {
const std::vector<ImageHandle> &renderTargets) {
std::vector<glm::mat4> matrices;
std::vector<DrawcallInfo> drawcalls;
size_t count = 0;
const glm::mat4 viewProjection = camera.getMVP();
for (auto& node : m_nodes) {
count += node.getDrawcallCount();
node.recordDrawcalls(viewProjection, matrices, drawcalls);
}
vkcv_log(LogLevel::RAW_INFO, "Frustum culling: %lu / %lu", drawcalls.size(), count);
PushConstantData pushConstantData (matrices.data(), sizeof(glm::mat4));
m_core->recordDrawcallsToCmdStream(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment