Skip to content

Commit 193e82e

Browse files
author
devsh
committed
remove getNormalPointer and generalize it
1 parent dd2d011 commit 193e82e

File tree

6 files changed

+91
-62
lines changed

6 files changed

+91
-62
lines changed

include/nbl/asset/ICPUPolygonGeometry.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,6 @@ class NBL_API2 ICPUPolygonGeometry final : public IPolygonGeometry<ICPUBuffer>
9797
}
9898
return false;
9999
}
100-
inline void* getNormalPtr()
101-
{
102-
if (isMutable())
103-
{
104-
return m_normalView.getPointer();
105-
}
106-
return nullptr;
107-
}
108100

109101
//
110102
template<typename Visitor>

include/nbl/asset/IGeometry.h

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,8 +404,34 @@ class IGeometry : public std::conditional_t<std::is_same_v<BufferType,ICPUBuffer
404404
SDataViewBase composed = {};
405405
SBufferRange<BufferType> src = {};
406406
};
407+
template<typename U=BufferType> requires (std::is_same_v<U,BufferType> && std::is_same_v<U,ICPUBuffer>)
408+
class MutableElementAccessor final
409+
{
410+
friend class IGeometry<ICPUBuffer>;
411+
inline MutableElementAccessor(SDataView* const pView) : _this(pView) {}
412+
413+
SDataView* const _this;
414+
415+
public:
416+
template<typename Index=uint32_t>
417+
inline void* getPointer(const Index elIx=0)
418+
{
419+
if (_this)
420+
return _this->getPointer<Index,BufferType>(elIx);
421+
return nullptr;
422+
}
423+
template<typename V, typename Index=uint32_t> requires hlsl::concepts::Vector<V>
424+
inline void encodeElement(const Index elIx, const V& v)
425+
{
426+
if (_this)
427+
_this->encodeElement<V,Index,BufferType>(elIx,v);
428+
}
429+
};
407430
//
408431
inline const SDataView& getPositionView() const {return m_positionView;}
432+
// TODO: the requires should really be done with `declval` callability checks
433+
template<typename _B=BufferType> requires (std::is_same_v<_B,BufferType> && std::is_same_v<_B,ICPUBuffer>)
434+
inline MutableElementAccessor<_B> getPositionElementAccessor() {return createElementAccessor<_B>(m_positionView);}
409435

410436
// depends on indexing, primitive type, etc.
411437
virtual uint64_t getPrimitiveCount() const = 0;
@@ -426,6 +452,11 @@ class IGeometry : public std::conditional_t<std::is_same_v<BufferType,ICPUBuffer
426452

427453
protected:
428454
virtual inline ~IGeometry() = default;
455+
template<typename _B> requires (std::is_same_v<_B,BufferType> && std::is_same_v<_B,ICPUBuffer>)
456+
inline MutableElementAccessor<_B> createElementAccessor(SDataView& view)
457+
{
458+
return MutableElementAccessor<_B>(IAsset::isMutable() ? (&view):nullptr);
459+
}
429460

430461
//
431462
inline bool setJointOBBView(SDataView&& view)
@@ -458,13 +489,17 @@ class IGeometry : public std::conditional_t<std::is_same_v<BufferType,ICPUBuffer
458489
template<class BufferType>
459490
class IIndexableGeometry : public IGeometry<BufferType>
460491
{
492+
using base_t = IGeometry<BufferType>;
493+
461494
protected:
462-
using SDataView = IGeometry<BufferType>::SDataView;
495+
using SDataView = base_t::SDataView;
463496

464497
public:
465498
// index buffer is optional so no override of `valid()`
466499

467500
inline const SDataView& getIndexView() const {return m_indexView;}
501+
template<typename _B=BufferType> requires (std::is_same_v<_B,BufferType> && std::is_same_v<_B,ICPUBuffer>)
502+
inline base_t::template MutableElementAccessor<_B> getIndexElementAccessor() {return base_t::template createElementAccessor<_B>(m_indexView);}
468503

469504
inline const uint64_t getIndexCount() const
470505
{

include/nbl/asset/IPolygonGeometry.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@ class IPolygonGeometry : public IIndexableGeometry<BufferType>, public IPolygonG
179179

180180
// For when the geometric normal of the patch isn't enough and you want interpolated custom normals
181181
inline const SDataView& getNormalView() const {return m_normalView;}
182+
template<typename _B=BufferType> requires (std::is_same_v<_B,BufferType> && std::is_same_v<_B,ICPUBuffer>)
183+
inline IGeometry<_B>::template MutableElementAccessor<_B> getNormalAccessor() {return base_t::template createElementAccessor<_B>(m_normalView);}
182184

183185
// Its also a Max Joint ID that `m_jointWeightViews` can reference
184186
inline uint32_t getJointCount() const override final

src/nbl/asset/utils/CPolygonGeometryManipulator.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,14 @@ core::smart_refctd_ptr<ICPUPolygonGeometry> CPolygonGeometryManipulator::createU
133133

134134
core::smart_refctd_ptr<ICPUPolygonGeometry> CPolygonGeometryManipulator::createSmoothVertexNormal(const ICPUPolygonGeometry* inPolygon, bool enableWelding, float epsilon, VxCmpFunction vxcmp)
135135
{
136-
if (inPolygon == nullptr)
136+
if (!inPolygon)
137137
{
138138
_NBL_DEBUG_BREAK_IF(true);
139139
return nullptr;
140140
}
141141

142-
//Mesh need to be unwelded
143-
if (inPolygon->getIndexView())
142+
// Mesh need to be unwelded (TODO: why? the output only need to be unwelded, really should be checking `inPolygon->getIndexingCallback()->count()!=3`)
143+
if (inPolygon->getIndexView() && inPolygon->getIndexingCallback()!=IPolygonGeometryBase::TriangleList())
144144
{
145145
_NBL_DEBUG_BREAK_IF(true);
146146
return nullptr;

src/nbl/asset/utils/CSmoothNormalGenerator.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -231,26 +231,26 @@ CSmoothNormalGenerator::VertexHashMap CSmoothNormalGenerator::setupData(const as
231231

232232
core::smart_refctd_ptr<ICPUPolygonGeometry> CSmoothNormalGenerator::processConnectedVertices(const asset::ICPUPolygonGeometry* polygon, VertexHashMap& vertexHashMap, float epsilon, CPolygonGeometryManipulator::VxCmpFunction vxcmp)
233233
{
234-
auto outPolygon = core::move_and_static_cast<ICPUPolygonGeometry>(polygon->clone(0u));
235-
static constexpr auto NormalFormat = EF_R32G32B32_SFLOAT;
236-
const auto normalFormatBytesize = asset::getTexelOrBlockBytesize(NormalFormat);
237-
auto normalBuf = ICPUBuffer::create({ normalFormatBytesize * outPolygon->getPositionView().getElementCount()});
238-
auto normalView = polygon->getNormalView();
239-
240-
hlsl::shapes::AABB<4,hlsl::float32_t> aabb;
241-
aabb.maxVx = hlsl::float32_t4(1, 1, 1, 0.f);
242-
aabb.minVx = -aabb.maxVx;
243-
outPolygon->setNormalView({
244-
.composed = {
245-
.encodedDataRange = {.f32 = aabb},
246-
.stride = sizeof(hlsl::float32_t3),
247-
.format = NormalFormat,
248-
.rangeFormat = IGeometryBase::EAABBFormat::F32
249-
},
250-
.src = { .offset = 0, .size = normalBuf->getSize(), .buffer = std::move(normalBuf) },
251-
});
252-
253-
auto* normalPtr = reinterpret_cast<std::byte*>(outPolygon->getNormalPtr());
234+
auto outPolygon = core::move_and_static_cast<ICPUPolygonGeometry>(polygon->clone(0u));
235+
static constexpr auto NormalFormat = EF_R32G32B32_SFLOAT;
236+
const auto normalFormatBytesize = asset::getTexelOrBlockBytesize(NormalFormat);
237+
auto normalBuf = ICPUBuffer::create({ normalFormatBytesize * outPolygon->getPositionView().getElementCount()});
238+
auto normalView = polygon->getNormalView();
239+
240+
hlsl::shapes::AABB<4,hlsl::float32_t> aabb;
241+
aabb.maxVx = hlsl::float32_t4(1, 1, 1, 0.f);
242+
aabb.minVx = -aabb.maxVx;
243+
outPolygon->setNormalView({
244+
.composed = {
245+
.encodedDataRange = {.f32 = aabb},
246+
.stride = sizeof(hlsl::float32_t3),
247+
.format = NormalFormat,
248+
.rangeFormat = IGeometryBase::EAABBFormat::F32
249+
},
250+
.src = { .offset = 0, .size = normalBuf->getSize(), .buffer = std::move(normalBuf) },
251+
});
252+
253+
auto* normalPtr = reinterpret_cast<std::byte*>(outPolygon->getNormalAccessor().getPointer());
254254
auto normalStride = outPolygon->getNormalView().composed.stride;
255255

256256
for (uint32_t cell = 0; cell < vertexHashMap.getBucketCount() - 1; cell++)

src/nbl/asset/utils/CSmoothNormalGenerator.h

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,45 +21,45 @@ class CSmoothNormalGenerator
2121
private:
2222
class VertexHashMap
2323
{
24-
public:
25-
struct BucketBounds
26-
{
27-
core::vector<CPolygonGeometryManipulator::SSNGVertexData>::iterator begin;
28-
core::vector<CPolygonGeometryManipulator::SSNGVertexData>::iterator end;
29-
};
24+
public:
25+
struct BucketBounds
26+
{
27+
core::vector<CPolygonGeometryManipulator::SSNGVertexData>::iterator begin;
28+
core::vector<CPolygonGeometryManipulator::SSNGVertexData>::iterator end;
29+
};
3030

31-
public:
32-
VertexHashMap(size_t _vertexCount, uint32_t _hashTableMaxSize, float _cellSize);
31+
public:
32+
VertexHashMap(size_t _vertexCount, uint32_t _hashTableMaxSize, float _cellSize);
3333

34-
//inserts vertex into hash table
35-
void add(CPolygonGeometryManipulator::SSNGVertexData&& vertex);
34+
//inserts vertex into hash table
35+
void add(CPolygonGeometryManipulator::SSNGVertexData&& vertex);
3636

37-
//sorts hashtable and sets iterators at beginnings of bucktes
38-
void validate();
37+
//sorts hashtable and sets iterators at beginnings of bucktes
38+
void validate();
3939

40-
inline uint32_t getVertexCount() const { return m_vertices.size(); }
40+
inline uint32_t getVertexCount() const { return m_vertices.size(); }
4141

42-
//
43-
std::array<uint32_t, 8> getNeighboringCellHashes(const CPolygonGeometryManipulator::SSNGVertexData& vertex);
42+
//
43+
std::array<uint32_t, 8> getNeighboringCellHashes(const CPolygonGeometryManipulator::SSNGVertexData& vertex);
4444

45-
inline uint32_t getBucketCount() { return m_buckets.size(); }
46-
inline BucketBounds getBucketBoundsById(uint32_t index) const { return { m_buckets[index], m_buckets[index + 1] }; }
47-
BucketBounds getBucketBoundsByHash(uint32_t hash);
45+
inline uint32_t getBucketCount() { return m_buckets.size(); }
46+
inline BucketBounds getBucketBoundsById(uint32_t index) const { return { m_buckets[index], m_buckets[index + 1] }; }
47+
BucketBounds getBucketBoundsByHash(uint32_t hash);
4848

49-
private:
50-
static constexpr uint32_t invalidHash = 0xFFFFFFFF;
51-
static constexpr uint32_t primeNumber1 = 73856093;
52-
static constexpr uint32_t primeNumber2 = 19349663;
53-
static constexpr uint32_t primeNumber3 = 83492791;
49+
private:
50+
static inline constexpr uint32_t invalidHash = 0xFFFFFFFF;
51+
static inline constexpr uint32_t primeNumber1 = 73856093;
52+
static inline constexpr uint32_t primeNumber2 = 19349663;
53+
static inline constexpr uint32_t primeNumber3 = 83492791;
5454

55-
//holds iterators pointing to beginning of each bucket, last iterator points to m_vertices.end()
56-
core::vector<core::vector<CPolygonGeometryManipulator::SSNGVertexData>::iterator> m_buckets;
57-
core::vector<CPolygonGeometryManipulator::SSNGVertexData> m_vertices;
58-
const uint32_t m_hashTableMaxSize;
59-
const float m_cellSize;
55+
//holds iterators pointing to beginning of each bucket, last iterator points to m_vertices.end()
56+
core::vector<core::vector<CPolygonGeometryManipulator::SSNGVertexData>::iterator> m_buckets;
57+
core::vector<CPolygonGeometryManipulator::SSNGVertexData> m_vertices;
58+
const uint32_t m_hashTableMaxSize;
59+
const float m_cellSize;
6060

61-
uint32_t hash(const CPolygonGeometryManipulator::SSNGVertexData& vertex) const;
62-
uint32_t hash(const hlsl::uint32_t3& position) const;
61+
uint32_t hash(const CPolygonGeometryManipulator::SSNGVertexData& vertex) const;
62+
uint32_t hash(const hlsl::uint32_t3& position) const;
6363

6464
};
6565

0 commit comments

Comments
 (0)