Skip to content

Commit 898b259

Browse files
author
devsh
committed
AABB recomputation function
1 parent 9186edc commit 898b259

File tree

4 files changed

+68
-11
lines changed

4 files changed

+68
-11
lines changed

include/nbl/asset/IGeometry.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,9 @@ class IGeometry : public std::conditional_t<std::is_same_v<BufferType,ICPUBuffer
294294
template<typename Index=uint32_t, typename U=BufferType> requires (std::is_same_v<U,BufferType> && std::is_same_v<U,ICPUBuffer>)
295295
inline const void* getPointer(const Index elIx=0) const
296296
{
297-
return const_cast<typename std::decay_t<decltype(*this)>*>(this)->getPointer<U>(elIx);
297+
if (*this)
298+
return reinterpret_cast<const uint8_t*>(src.buffer->getPointer())+src.offset+elIx*composed.getStride();
299+
return nullptr;
298300
}
299301
template<typename Index=uint32_t, typename U=BufferType> requires (std::is_same_v<U,BufferType> && std::is_same_v<U,ICPUBuffer>)
300302
inline void* getPointer(const Index elIx=0)
@@ -318,11 +320,16 @@ class IGeometry : public std::conditional_t<std::is_same_v<BufferType,ICPUBuffer
318320
assert(!isScaledFormat(composed.format)); // handle this by improving the decode functions, not adding workarounds here
319321
if (decodePixels<code_t>(composed.format,srcArr,tmp,0,0))
320322
{
321-
if (isNormalizedFormat(composed.format))
323+
using traits = hlsl::vector_traits<V>;
324+
const auto range = composed.getRange<hlsl::shapes::AABB<traits::Dimension,typename traits::scalar_type>>();
325+
for (auto i=0u; i<traits::Dimension; i++)
322326
{
323-
using traits = hlsl::vector_traits<V>;
324-
const auto range = composed.getRange<hlsl::shapes::AABB<traits::Dimension,traits::scalar_type>>();
325-
v = v*(range.maxVx-range.minVx)+range.minVx;
327+
if (isNormalizedFormat(composed.format))
328+
{
329+
v[i] = tmp[i] * (range.maxVx[i] - range.minVx[i]) + range.minVx[i];
330+
}
331+
else
332+
v[i] = tmp[i];
326333
}
327334
return true;
328335
}
@@ -342,7 +349,7 @@ class IGeometry : public std::conditional_t<std::is_same_v<BufferType,ICPUBuffer
342349
using traits = hlsl::vector_traits<V>;
343350
using code_t = std::conditional_t<hlsl::concepts::FloatingPointVector<V>,hlsl::float64_t,std::conditional_t<hlsl::concepts::SignedIntVector<V>,int64_t,uint64_t>>;
344351
code_t tmp[traits::Dimension];
345-
const auto range = composed.getRange<traits::Dimension,traits::scalar_type>();
352+
const auto range = composed.getRange<traits::Dimension,typename traits::scalar_type>();
346353
for (auto i=0u; i<traits::Dimension; i++)
347354
{
348355
if (isNormalizedFormat(composed.format))

include/nbl/asset/utils/CPolygonGeometryManipulator.h

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,36 @@ class NBL_API2 CGeometryManipulator
2525
view.src.buffer->setContentHash(view.src.buffer->computeContentHash());
2626
}
2727

28-
// TODO: static inline IGeometryBase::SDataViewBase::SAABBStorage computeRange(const IGeometry<ICPUBuffer>::SDataView& view)
28+
static inline IGeometryBase::SAABBStorage computeRange(const IGeometry<ICPUBuffer>::SDataView& view)
29+
{
30+
if (!view || !view.composed.isFormatted())
31+
return {};
32+
auto it = reinterpret_cast<char*>(view.src.buffer->getPointer())+view.src.offset;
33+
const auto end = it+view.src.actualSize();
34+
auto addToAABB = [&](auto& aabb)->void
35+
{
36+
using aabb_t = std::remove_reference_t<decltype(aabb)>;
37+
for (auto i=0; i!=view.getElementCount(); i++)
38+
{
39+
typename aabb_t::point_t pt;
40+
view.decodeElement(i,pt);
41+
aabb.addPoint(pt);
42+
}
43+
};
44+
IGeometryBase::SDataViewBase tmp = {};
45+
tmp.resetRange(view.composed.rangeFormat);
46+
tmp.visitAABB(addToAABB);
47+
return tmp.encodedDataRange;
48+
}
2949

30-
// TODO: static inline void recomputeRange(IGeometry<ICPUBuffer>::SDataView& view)
50+
static inline void recomputeRange(IGeometry<ICPUBuffer>::SDataView& view, const bool deduceRangeFormat=true)
51+
{
52+
if (!view || !view.composed.isFormatted())
53+
return;
54+
if (deduceRangeFormat)
55+
view.composed.rangeFormat = IGeometryBase::getMatchingAABBFormat(view.composed.format);
56+
view.composed.encodedDataRange = computeRange(view);
57+
}
3158
};
3259

3360
//! An interface for easy manipulation of polygon geometries.
@@ -52,7 +79,28 @@ class NBL_API2 CPolygonGeometryManipulator
5279
CGeometryManipulator::recomputeContentHash(view);
5380
}
5481

55-
// TODO: recomputeRanges
82+
//
83+
static inline void recomputeRanges(ICPUPolygonGeometry* geo, const bool deduceRangeFormats=true)
84+
{
85+
if (!geo)
86+
return;
87+
auto recomputeRange = [deduceRangeFormats](const IGeometry<ICPUBuffer>::SDataView& view)->void
88+
{
89+
CGeometryManipulator::recomputeRange(const_cast<IGeometry<ICPUBuffer>::SDataView&>(view),deduceRangeFormats);
90+
};
91+
recomputeRange(geo->getPositionView());
92+
recomputeRange(geo->getIndexView());
93+
recomputeRange(geo->getNormalView());
94+
for (const auto& view : *geo->getJointWeightViews())
95+
{
96+
recomputeRange(view.indices);
97+
recomputeRange(view.weights);
98+
}
99+
if (auto pView=geo->getJointOBBView(); pView)
100+
recomputeRange(*pView);
101+
for (const auto& view : *geo->getAuxAttributeViews())
102+
recomputeRange(view);
103+
}
56104

57105
//! Comparison methods
58106
enum E_ERROR_METRIC

include/nbl/builtin/hlsl/shapes/aabb.hlsl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace shapes
1818
template<int16_t D=3, typename Scalar=float32_t>
1919
struct AABB
2020
{
21+
using scalar_t = Scalar;
2122
using point_t = vector<Scalar,D>;
2223

2324
static AABB create()
@@ -31,8 +32,8 @@ struct AABB
3132
//
3233
void addPoint(const point_t pt)
3334
{
34-
minVx = min<point_t>(pt,minVx);
35-
maxVx = max<point_t>(pt,maxVx);
35+
minVx = hlsl::min<point_t>(pt,minVx);
36+
maxVx = hlsl::max<point_t>(pt,maxVx);
3637
}
3738
//
3839
point_t getExtent()

src/nbl/asset/interchange/CPLYMeshFileLoader.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,7 @@ SAssetBundle CPLYMeshFileLoader::loadAsset(system::IFile* _file, const IAssetLoa
898898
}
899899

900900
CPolygonGeometryManipulator::recomputeContentHashes(geometry.get());
901+
CPolygonGeometryManipulator::recomputeRanges(geometry.get());
901902

902903
auto meta = core::make_smart_refctd_ptr<CPLYMetadata>();
903904
return SAssetBundle(std::move(meta),{std::move(geometry)});

0 commit comments

Comments
 (0)