Skip to content

Commit

Permalink
Merge pull request #121 from godlikepanos/gpu_occlusion
Browse files Browse the repository at this point in the history
GPU based occlusion
  • Loading branch information
godlikepanos authored Oct 2, 2023
2 parents fd12a5f + e808248 commit fe26924
Show file tree
Hide file tree
Showing 546 changed files with 17,563 additions and 17,589 deletions.
4 changes: 2 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ BreakConstructorInitializersBeforeComma: true
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 120
ColumnLimit: 150
CommentPragmas: '^ IWYU pragma:'
QualifierAlignment: Leave
CompactNamespaces: false
Expand Down Expand Up @@ -122,7 +122,7 @@ ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakFirstLessLess: 150
PenaltyBreakOpenParenthesis: 0
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
Expand Down
4 changes: 2 additions & 2 deletions .clang-format-hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 120
ColumnLimit: 150
CommentPragmas: '^ IWYU pragma:'
QualifierAlignment: Leave
CompactNamespaces: false
Expand Down Expand Up @@ -122,7 +122,7 @@ ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakFirstLessLess: 150
PenaltyBreakOpenParenthesis: 0
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
Expand Down
13 changes: 1 addition & 12 deletions AnKi.natvis
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,6 @@
</Expand>
</Type>

<Type Name="anki::DynamicArrayRaii&lt;*&gt;">
<Intrinsic Name="size" Expression="m_size" />
<DisplayString>{{ size={m_size} capacity={m_capacity} }}</DisplayString>
<Expand>
<ArrayItems>
<Size>m_size</Size>
<ValuePointer>m_data</ValuePointer>
</ArrayItems>
</Expand>
</Type>

<Type Name="anki::Array&lt;*,*&gt;">
<DisplayString>{{ size={$T2} }}</DisplayString>
<Expand>
Expand All @@ -62,7 +51,7 @@
<DisplayString>{m_data.m_data,s}</DisplayString>
</Type>

<Type Name="anki::BaseStringRaii&lt;*&gt;">
<Type Name="anki::BaseString&lt;*&gt;">
<DisplayString Condition="(bool)(m_data.m_data==0)">*Empty*</DisplayString>
<DisplayString>{m_data.m_data,s}</DisplayString>
</Type>
Expand Down
3 changes: 1 addition & 2 deletions AnKi/Collision/Aabb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ Aabb Aabb::getCompoundShape(const Aabb& b) const
return out;
}

void Aabb::setFromPointCloud(const Vec3* pointBuffer, U pointCount, PtrSize pointStride,
[[maybe_unused]] PtrSize buffSize)
void Aabb::setFromPointCloud(const Vec3* pointBuffer, U pointCount, PtrSize pointStride, [[maybe_unused]] PtrSize buffSize)
{
// Preconditions
ANKI_ASSERT(pointBuffer);
Expand Down
4 changes: 2 additions & 2 deletions AnKi/Collision/Aabb.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ class Aabb

private:
Vec4 m_min
#if ANKI_ENABLE_ASSERTIONS
#if ANKI_ASSERTIONS_ENABLED
= Vec4(kMaxF32)
#endif
;

Vec4 m_max
#if ANKI_ENABLE_ASSERTIONS
#if ANKI_ASSERTIONS_ENABLED
= Vec4(kMinF32)
#endif
;
Expand Down
8 changes: 4 additions & 4 deletions AnKi/Collision/Cone.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,25 +106,25 @@ class Cone

private:
Vec4 m_origin
#if ANKI_ENABLE_ASSERTIONS
#if ANKI_ASSERTIONS_ENABLED
= Vec4(kMaxF32)
#endif
;

Vec4 m_dir
#if ANKI_ENABLE_ASSERTIONS
#if ANKI_ASSERTIONS_ENABLED
= Vec4(kMaxF32)
#endif
;

F32 m_length
#if ANKI_ENABLE_ASSERTIONS
#if ANKI_ASSERTIONS_ENABLED
= -1.0f
#endif
;

F32 m_angle
#if ANKI_ENABLE_ASSERTIONS
#if ANKI_ASSERTIONS_ENABLED
= -1.0f
#endif
;
Expand Down
4 changes: 2 additions & 2 deletions AnKi/Collision/ConvexHullShape.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ class ConvexHullShape
Transform m_invTrf;

const Vec4* m_points
#if ANKI_ENABLE_ASSERTIONS
#if ANKI_ASSERTIONS_ENABLED
= nullptr
#endif
;

U32 m_pointCount
#if ANKI_ENABLE_ASSERTIONS
#if ANKI_ASSERTIONS_ENABLED
= 0
#endif
;
Expand Down
6 changes: 5 additions & 1 deletion AnKi/Collision/Functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <AnKi/Collision/Plane.h>
#include <AnKi/Collision/Ray.h>
#include <AnKi/Collision/Aabb.h>
#include <AnKi/Util/WeakArray.h>

namespace anki {

Expand Down Expand Up @@ -225,7 +226,10 @@ void extractClipPlanes(const Mat4& mvp, Array<Plane, 6>& planes);
void extractClipPlane(const Mat4& mvp, FrustumPlaneType id, Plane& plane);

/// Compute the edges of the far plane of a frustum
void computeEdgesOfFrustum(F32 far, F32 fovX, F32 fovY, Vec4 points[4]);
void computeEdgesOfFrustum(F32 far, F32 fovX, F32 fovY, Vec3 points[4]);

/// Welzl's algorithm that computes a compact bounding sphere given a point cloud.
Sphere computeBoundingSphere(ConstWeakArray<Vec3> points);

/// @}

Expand Down
158 changes: 152 additions & 6 deletions AnKi/Collision/FunctionsMisc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
// http://www.anki3d.org/LICENSE

#include <AnKi/Collision/Functions.h>
#include <AnKi/Collision/Sphere.h>

namespace anki {

void extractClipPlane(const Mat4& mvp, FrustumPlaneType id, Plane& plane)
{
// This code extracts the planes assuming the projection matrices are DX-like right-handed (eg D3DXMatrixPerspectiveFovRH)
// See "Fast Extraction of Viewing Frustum Planes from the WorldView-Projection Matrix" paper for more info

#define ANKI_CASE(i, a, op, b) \
case i: \
{ \
Expand All @@ -21,7 +25,14 @@ void extractClipPlane(const Mat4& mvp, FrustumPlaneType id, Plane& plane)

switch(id)
{
ANKI_CASE(FrustumPlaneType::kNear, 3, +, 2)
case FrustumPlaneType::kNear:
{
const Vec4 planeEqationCoefs = mvp.getRow(2);
const Vec4 n = planeEqationCoefs.xyz0();
const F32 len = n.getLength();
plane = Plane(n / len, -planeEqationCoefs.w() / len);
break;
}
ANKI_CASE(FrustumPlaneType::kFar, 3, -, 2)
ANKI_CASE(FrustumPlaneType::kLeft, 3, +, 0)
ANKI_CASE(FrustumPlaneType::kRight, 3, -, 0)
Expand All @@ -42,17 +53,152 @@ void extractClipPlanes(const Mat4& mvp, Array<Plane, 6>& planes)
}
}

void computeEdgesOfFrustum(F32 far, F32 fovX, F32 fovY, Vec4 points[4])
void computeEdgesOfFrustum(F32 far, F32 fovX, F32 fovY, Vec3 points[4])
{
// This came from unprojecting. It works, don't touch it
const F32 x = far * tan(fovY / 2.0f) * fovX / fovY;
const F32 y = tan(fovY / 2.0f) * far;
const F32 z = -far;

points[0] = Vec4(x, y, z, 0.0f); // top right
points[1] = Vec4(-x, y, z, 0.0f); // top left
points[2] = Vec4(-x, -y, z, 0.0f); // bot left
points[3] = Vec4(x, -y, z, 0.0f); // bot right
points[0] = Vec3(x, y, z); // top right
points[1] = Vec3(-x, y, z); // top left
points[2] = Vec3(-x, -y, z); // bot left
points[3] = Vec3(x, -y, z); // bot right
}

Vec4 computeBoundingSphereRecursive(WeakArray<const Vec3*> pPoints, U32 begin, U32 p, U32 b)
{
Vec4 sphere;

switch(b)
{
case 0:
sphere = Vec4(0.0f, 0.0f, 0.0f, -1.0f);
break;
case 1:
sphere = Vec4(*pPoints[begin - 1], kEpsilonf);
break;
case 2:
{
const Vec3 O = *pPoints[begin - 1];
const Vec3 A = *pPoints[begin - 2];

const Vec3 a = A - O;

const Vec3 o = 0.5f * a;

const F32 radius = o.getLength() + kEpsilonf;
const Vec3 center = O + o;

sphere = Vec4(center, radius);
break;
}
case 3:
{
const Vec3 O = *pPoints[begin - 1];
const Vec3 A = *pPoints[begin - 2];
const Vec3 B = *pPoints[begin - 3];

const Vec3 a = A - O;
const Vec3 b = B - O;

const Vec3 acrossb = a.cross(b);
const F32 denominator = 2.0f * (acrossb.dot(acrossb));

Vec3 o = b.dot(b) * acrossb.cross(a);
o += a.dot(a) * b.cross(acrossb);
o /= denominator;

const F32 radius = o.getLength() + kEpsilonf;
const Vec3 center = O + o;

sphere = Vec4(center, radius);

return sphere;
}
#if 0
// There is this case as well but it fails if points are coplanar so avoid it
case 4:
{
const Vec3 O = *pPoints[begin - 1];
const Vec3 A = *pPoints[begin - 2];
const Vec3 B = *pPoints[begin - 3];
const Vec3 C = *pPoints[begin - 4];

const Vec3 a = A - O;
const Vec3 b = B - O;
const Vec3 c = C - O;

auto compDet = [](F32 m11, F32 m12, F32 m13, F32 m21, F32 m22, F32 m23, F32 m31, F32 m32, F32 m33) {
return m11 * (m22 * m33 - m32 * m23) - m21 * (m12 * m33 - m32 * m13) + m31 * (m12 * m23 - m22 * m13);
};

const F32 denominator = 2.0f * compDet(a.x(), a.y(), a.z(), b.x(), b.y(), b.z(), c.x(), c.y(), c.z());

Vec3 o = c.dot(c) * a.cross(b);
o += b.dot(b) * c.cross(a);
o += a.dot(a) * b.cross(c);
o /= denominator;

const F32 radius = o.getLength() + kEpsilonf;
const Vec3 center = O + o;

sphere = Vec4(center, radius);

return sphere;
}
#endif
default:
ANKI_ASSERT(0);
}

for(U32 i = 0; i < p; i++)
{
const F32 distSq = (sphere.xyz() - *pPoints[begin + i]).getLengthSquared();
const F32 radiusSq = sphere.w() * sphere.w();

if(distSq > radiusSq)
{
for(U32 j = i; j > 0; j--)
{
const Vec3* T = pPoints[begin + j];
pPoints[begin + j] = pPoints[begin + j - 1];
pPoints[begin + j - 1] = T;
}

sphere = computeBoundingSphereRecursive(pPoints, begin + 1, i, b + 1);
}
}

return sphere;
}

Sphere computeBoundingSphere(ConstWeakArray<Vec3> points)
{
ANKI_ASSERT(points.getSize() >= 3);

DynamicArray<const Vec3*> pPointsDyn;
Array<const Vec3*, 8> pPointsArr;
WeakArray<const Vec3*> pPoints;

if(points.getSize() > pPointsArr.getSize())
{
pPointsDyn.resize(points.getSize());
pPoints = pPointsDyn;
}
else
{
pPoints = {&pPointsArr[0], points.getSize()};
}

for(U32 i = 0; i < points.getSize(); ++i)
{
pPoints[i] = &points[i];
}

const Vec4 sphere = computeBoundingSphereRecursive(pPoints, 0, pPoints.getSize(), 0);

return Sphere(sphere.xyz(), sphere.w());
}

} // end namespace anki
6 changes: 2 additions & 4 deletions AnKi/Collision/FunctionsTestPlane.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ F32 testPlane(const Plane& plane, const Aabb& aabb)
__m128 gezero = _mm_cmpge_ps(plane.getNormal().getSimd(), _mm_setzero_ps());

Vec4 diagMin;
diagMin.getSimd() =
_mm_or_ps(_mm_and_ps(gezero, aabb.getMin().getSimd()), _mm_andnot_ps(gezero, aabb.getMax().getSimd()));
diagMin.getSimd() = _mm_or_ps(_mm_and_ps(gezero, aabb.getMin().getSimd()), _mm_andnot_ps(gezero, aabb.getMax().getSimd()));
#else
Vec4 diagMin(0.0f), diagMax(0.0f);
// set min/max values for x,y,z direction
Expand Down Expand Up @@ -43,8 +42,7 @@ F32 testPlane(const Plane& plane, const Aabb& aabb)

#if ANKI_SIMD_SSE
Vec4 diagMax;
diagMax.getSimd() =
_mm_or_ps(_mm_and_ps(gezero, aabb.getMax().getSimd()), _mm_andnot_ps(gezero, aabb.getMin().getSimd()));
diagMax.getSimd() = _mm_or_ps(_mm_and_ps(gezero, aabb.getMax().getSimd()), _mm_andnot_ps(gezero, aabb.getMin().getSimd()));
#endif

ANKI_ASSERT(diagMax.w() == 0.0f);
Expand Down
3 changes: 1 addition & 2 deletions AnKi/Collision/GjkEpa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,7 @@ static Bool update(GjkContext& ctx, const GjkSupport& a)
return true;
}

Bool gjkIntersection(const void* shape0, GjkSupportCallback shape0Callback, const void* shape1,
GjkSupportCallback shape1Callback)
Bool gjkIntersection(const void* shape0, GjkSupportCallback shape0Callback, const void* shape1, GjkSupportCallback shape1Callback)
{
ANKI_ASSERT(shape0 && shape0Callback && shape1 && shape1Callback);

Expand Down
3 changes: 1 addition & 2 deletions AnKi/Collision/GjkEpa.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ namespace anki {
using GjkSupportCallback = Vec4 (*)(const void* shape, const Vec4& dir);

/// Return true if the two convex shapes intersect.
Bool gjkIntersection(const void* shape0, GjkSupportCallback shape0Callback, const void* shape1,
GjkSupportCallback shape1Callback);
Bool gjkIntersection(const void* shape0, GjkSupportCallback shape0Callback, const void* shape1, GjkSupportCallback shape1Callback);
/// @}

} // end namespace anki
Loading

0 comments on commit fe26924

Please sign in to comment.