Skip to content

Commit 18adf44

Browse files
author
devsh
committed
hoist most of the BLAS geometry export out of IGeometryCollection
Add `valid()` methods to `IGeometry` and all base classes Correct the dependant visits so null deps aren't visited Finally correct the setters of `SDataView`s to allow for clearing
1 parent 7f1302c commit 18adf44

File tree

4 files changed

+118
-76
lines changed

4 files changed

+118
-76
lines changed

include/nbl/asset/ICPUPolygonGeometry.h

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,30 @@
55
#define _NBL_ASSET_I_CPU_POLYGON_GEOMETRY_H_INCLUDED_
66

77

8+
#include "nbl/asset/IAsset.h"
9+
#include "nbl/asset/ICPUBuffer.h"
810
#include "nbl/asset/IPolygonGeometry.h"
911

1012

1113
namespace nbl::asset
1214
{
1315
//
14-
class NBL_API2 ICPUPolygonGeometry : public IAsset, public IPolygonGeometry<ICPUBuffer>
16+
class NBL_API2 ICPUPolygonGeometry final : public IAsset, public IPolygonGeometry<ICPUBuffer>
1517
{
1618
using base_t = IPolygonGeometry<ICPUBuffer>;
1719

20+
protected:
21+
using SDataView = base_t::SDataView;
22+
1823
public:
1924
inline ICPUPolygonGeometry() = default;
2025

2126
constexpr static inline auto AssetType = ET_GEOMETRY;
2227
inline E_TYPE getAssetType() const override {return AssetType;}
2328

24-
inline core::smart_refctd_ptr<IAsset> clone(uint32_t _depth=~0u) const
29+
inline bool valid() const override {return base_t::valid();}
30+
31+
inline core::smart_refctd_ptr<IAsset> clone(uint32_t _depth=~0u) const override
2532
{
2633
const auto nextDepth = _depth ? (_depth-1):0;
2734
auto retval = core::smart_refctd_ptr<ICPUPolygonGeometry>();
@@ -48,7 +55,7 @@ class NBL_API2 ICPUPolygonGeometry : public IAsset, public IPolygonGeometry<ICPU
4855
inline size_t getDependantCount() const override
4956
{
5057
size_t count = 0;
51-
visitDependents([&current](const IAsset* dep)->bool
58+
visitDependents([&count](const IAsset* dep)->bool
5259
{
5360
count++;
5461
return true;
@@ -57,10 +64,10 @@ class NBL_API2 ICPUPolygonGeometry : public IAsset, public IPolygonGeometry<ICPU
5764
return count;
5865
}
5966

60-
// needs to be hidden because of mutability checking
67+
//
6168
inline bool setPositionView(SDataView&& view)
6269
{
63-
if (isMutable() && view.composed.isFormatted())
70+
if (isMutable() && (!view || view.composed.isFormatted()))
6471
return base_t::setPositionView(std::move(view));
6572
return false;
6673
}
@@ -73,7 +80,7 @@ class NBL_API2 ICPUPolygonGeometry : public IAsset, public IPolygonGeometry<ICPU
7380
return false;
7481
}
7582

76-
// Needs to be hidden because ICPU base class shall check mutability
83+
//
7784
inline bool setIndexView(SDataView&& view)
7885
{
7986
if (isMutable())
@@ -104,9 +111,9 @@ class NBL_API2 ICPUPolygonGeometry : public IAsset, public IPolygonGeometry<ICPU
104111
}
105112

106113
//
107-
inline void setNormalView(SDataView&& view)
114+
inline bool setNormalView(SDataView&& view)
108115
{
109-
if (isMutable())
116+
if (isMutable() && (!view || view.composed.getStride()>0))
110117
{
111118
m_normalView = std::move(view);
112119
return true;
@@ -126,43 +133,49 @@ class NBL_API2 ICPUPolygonGeometry : public IAsset, public IPolygonGeometry<ICPU
126133
}
127134

128135
//
129-
inline const core::vector<SJointWeight>* getJointWeightViews()
136+
inline core::vector<SJointWeight>* getJointWeightViews()
130137
{
131138
if (isMutable())
132-
return m_jointWeightViews;
139+
return &m_jointWeightViews;
133140
return nullptr;
134141
}
135142

136143
//
137-
inline const core::vector<SJointWeight>* getAuxAttributeViews()
144+
inline core::vector<SDataView>* getAuxAttributeViews()
138145
{
139146
if (isMutable())
140-
return m_auxAttributeViews;
147+
return &m_auxAttributeViews;
141148
return nullptr;
142149
}
143150

144151
protected:
145152
//
146-
inline void visitDependents(std::function<bool(const IAsset*)>& visit) const //override
153+
inline void visitDependents(std::function<bool(const IAsset*)> visit) const //override
147154
{
148-
if (!visit(m_positionView.src.buffer.get())) return;
149-
if (!visit(m_jointOBBView.src.buffer.get())) return;
150-
if (!visit(m_indexView.src.buffer.get())) return;
155+
auto nonNullOnly = [&visit](const IAsset* dep)->bool
156+
{
157+
if (dep)
158+
return visit(dep);
159+
return true;
160+
};
161+
if (!nonNullOnly(m_positionView.src.buffer.get())) return;
162+
if (!nonNullOnly(m_jointOBBView.src.buffer.get())) return;
163+
if (!nonNullOnly(m_indexView.src.buffer.get())) return;
151164
for (const auto& pair : m_jointWeightViews)
152165
{
153-
if (!visit(pair.indices.src.buffer.get())) return;
154-
if (!visit(pair.weights.src.buffer.get())) return;
166+
if (!nonNullOnly(pair.indices.src.buffer.get())) return;
167+
if (!nonNullOnly(pair.weights.src.buffer.get())) return;
155168
}
156169
for (const auto& view : m_auxAttributeViews)
157-
if (!visit(view.src.buffer.get())) return;
158-
if (!visit(m_normalView.src.buffer.get())) return;
170+
if (!nonNullOnly(view.src.buffer.get())) return;
171+
if (!nonNullOnly(m_normalView.src.buffer.get())) return;
159172
}
160173
// TODO: remove after https://github.com/Devsh-Graphics-Programming/Nabla/pull/871 merge
161174
inline IAsset* getDependant_impl(const size_t ix) override
162175
{
163176
const IAsset* retval = nullptr;
164177
size_t current = 0;
165-
visitDependents([&current](const IAsset* dep)->bool
178+
visitDependents([&](const IAsset* dep)->bool
166179
{
167180
retval = dep;
168181
return ix<current++;

include/nbl/asset/IGeometry.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ namespace nbl::asset
1515
class IGeometryBase : public virtual core::IReferenceCounted
1616
{
1717
public:
18+
// used for same purpose as and overlaps `IAsset::valid()`
19+
virtual bool valid() const = 0;
20+
1821
enum class EPrimitiveType : uint8_t
1922
{
2023
Polygon = 0,
@@ -196,6 +199,17 @@ template<class BufferType>
196199
class NBL_API2 IGeometry : public IGeometryBase
197200
{
198201
public:
202+
//
203+
virtual bool valid() const override
204+
{
205+
if (!m_positionView)
206+
return false;
207+
if (getPrimitiveCount()==0)
208+
return false;
209+
// joint OBBs are optional
210+
return true;
211+
}
212+
199213
struct SDataView
200214
{
201215
inline operator bool() const {return src && composed;}
@@ -363,7 +377,12 @@ class NBL_API2 IGeometry : public IGeometryBase
363377
template<class BufferType>
364378
class NBL_API2 IIndexableGeometry : public IGeometry<BufferType>
365379
{
380+
protected:
381+
using SDataView = IGeometry<BufferType>::SDataView;
382+
366383
public:
384+
// index buffer is optional so no override of `valid()`
385+
367386
inline const SDataView& getIndexView() const {return m_indexView;}
368387

369388
inline const uint64_t getIndexCount() const

include/nbl/asset/IGeometryCollection.h

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class NBL_API2 IGeometryCollection : public virtual core::IReferenceCounted
7777
return false;
7878
const auto matrixRowCount = inverseBindPoseView.getElementCount();
7979
// and 3 elements per matrix
80-
if (matrixRowCount==0 || matrixRowCount%3!=0)
80+
if (matrixRowCount==0 || (matrixRowCount%3)!=0)
8181
return false;
8282
const auto jointCount = matrixRowCount/3;
8383
// ok now check the AABB stuff
@@ -107,39 +107,9 @@ class NBL_API2 IGeometryCollection : public virtual core::IReferenceCounted
107107
if (geo->getPrimitiveType()==IGeometryBase::EPrimitiveType::Polygon)
108108
continue;
109109
const auto* polyGeo = static_cast<const IPolygonGeometry<BufferType>*>(geo);
110-
// not a triangle list
111-
if (polyGeo->getVerticesForFirst()!=3 || polyGeo->getVerticesPerSupplementary()!=3)
112-
continue;
113-
const auto& indexView = polyGeo->getIndexView();
114-
auto indexType = EIT_UNKNOWN;
115-
// disallowed index format
116-
if (indexView)
117-
{
118-
switch (indexView.format)
119-
{
120-
case EF_R16_UINT:
121-
indexType = EIT_16BIT;
122-
break;
123-
case EF_R32_UINT: [[fallthrough]];
124-
indexType = EIT_32BIT;
125-
break;
126-
default:
127-
break;
128-
}
129-
if (indexType==EIT_UNKNOWN)
130-
continue;
131-
}
132-
setTransform(out->transform,polyGeo->transform);
133-
const auto& positionView = polyGeo->getPositionView();
134-
out->vertexData[0] = positionView;
135-
out->vertexData[1] = {};
136-
out->indexData = indexView;
137-
out->maxVertex = positionView.getElementCount();
138-
out->vertexStride = positionView.getStride();
139-
out->vertexFormat = positionView.format;
140-
out->indexType = indexType;
141-
out->geometryFlags = IBottomLevelAccelerationStructure::GEOMETRY_FLAGS::NONE;
142-
out++;
110+
*out = polyGeo->exportForBLAS();
111+
if (out->vertexData[0])
112+
out++;
143113
}
144114
return out;
145115
}

include/nbl/asset/IPolygonGeometry.h

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,36 @@ namespace nbl::asset
1616
template<class BufferType>
1717
class NBL_API2 IPolygonGeometry : public IIndexableGeometry<BufferType>
1818
{
19-
using SDataView = IGeometry<BufferType>::SDataView;
19+
using base_t = IIndexableGeometry<BufferType>;
20+
21+
protected:
22+
using BLASTriangles = IBottomLevelAccelerationStructure::Triangles<std::remove_const<BufferType>>;
2023

2124
public:
25+
//
26+
virtual inline bool valid() const override
27+
{
28+
if (!base_t::valid())
29+
return false;
30+
// things that make no sense
31+
if (m_verticesPerSupplementary==0 || m_verticesPerSupplementary>m_verticesForFirst)
32+
return false;
33+
// there needs to be at least one vertex to reference (it also needs to be formatted)
34+
if (m_positionView.getElementCount()==0)
35+
return false;
36+
const auto vertexCount = m_positionView.getElementCount();
37+
if (m_normalView && m_normalView.getElementCount()<vertexCount)
38+
return false;
39+
// the variable length vectors must be filled with valid views
40+
for (const auto& pair : m_jointWeightViews)
41+
if (!pair || !pair.weights.getElementCount()<vertexCount)
42+
return false;
43+
for (const auto& view : m_auxAttributeViews)
44+
if (!view)
45+
return false;
46+
return true;
47+
}
48+
2249
//
2350
constexpr static inline EPrimitiveType PrimitiveType = EPrimitiveType::Polygon;
2451
inline EPrimitiveType getPrimitiveType() const override final {return PrimitiveType;}
@@ -70,27 +97,40 @@ class NBL_API2 IPolygonGeometry : public IIndexableGeometry<BufferType>
7097
inline uint16_t getVerticesForFirst() const {return m_verticesForFirst;}
7198
inline uint16_t getVerticesPerSupplementary() const {return m_verticesPerSupplementary;}
7299

73-
//
74-
inline bool valid() const
100+
// Does not set the `transform` or `geometryFlags` fields, because it doesn't care about it.
101+
// Also won't set second set of vertex data, opacity mipmaps, etc.
102+
inline BLASTriangles exportForBLAS() const
75103
{
76-
// things that make no sense
77-
if (m_verticesPerSupplementary==0 || m_verticesPerSupplementary>m_verticesForFirst)
78-
return false;
79-
// for polygons we must have the vertices formatted
80-
if (!m_positionView.isFormatted())
81-
return false;
82-
//
83-
const auto vertexCount = m_positionView.getElementCount();
84-
if (m_normalView && m_normalView.getElementCount()<vertexCount)
85-
return false;
86-
// the variable length vectors must be filled with valid views
87-
for (const auto& pair : m_jointWeightViews)
88-
if (!pair || pair.weights.getElementCount()<vertexCount)
89-
return false;
90-
for (const auto& view : m_auxAttributeViews)
91-
if (!view)
92-
return false;
93-
return true;
104+
BLASTriangles retval = {};
105+
// must be a triangle list
106+
if (m_verticesForFirst==3 && m_verticesPerSupplementary==3)
107+
{
108+
auto indexType = EIT_UNKNOWN;
109+
// disallowed index format
110+
if (m_indexView)
111+
{
112+
switch (m_indexView.format)
113+
{
114+
case EF_R16_UINT:
115+
indexType = EIT_16BIT;
116+
break;
117+
case EF_R32_UINT: [[fallthrough]];
118+
indexType = EIT_32BIT;
119+
break;
120+
default:
121+
break;
122+
}
123+
if (indexType==EIT_UNKNOWN)
124+
return retval;
125+
}
126+
retval.vertexData[0] = m_positionView;
127+
retval.indexData = m_indexView;
128+
retval.maxVertex = m_positionView.getElementCount();
129+
retval.vertexStride = m_positionView.getStride();
130+
retval.vertexFormat = m_positionView.format;
131+
retval.indexType = indexType;
132+
}
133+
return retval;
94134
}
95135

96136
protected:

0 commit comments

Comments
 (0)