@@ -18,40 +18,45 @@ namespace nbl::asset
1818core::smart_refctd_ptr<ICPUPolygonGeometry> CPolygonGeometryManipulator::createUnweldedList (const ICPUPolygonGeometry* inGeo)
1919{
2020 const auto * indexing = inGeo->getIndexingCallback ();
21- if (!indexing) return nullptr ;
22- if (indexing-> degree () != 3 ) return nullptr ;
21+ if (!indexing)
22+ return nullptr ;
2323
2424 const auto indexView = inGeo->getIndexView ();
2525 const auto primCount = inGeo->getPrimitiveCount ();
26+ const uint8_t degree = indexing->degree ();
27+ const auto outIndexCount = primCount*degree;
28+ if (outIndexCount<primCount)
29+ return nullptr ;
2630
2731 const auto outGeometry = core::move_and_static_cast<ICPUPolygonGeometry>(inGeo->clone (0u ));
2832
2933 auto * outGeo = outGeometry.get ();
30- outGeo->setIndexing (IPolygonGeometryBase::TriangleList ( ));
34+ outGeo->setIndexing (IPolygonGeometryBase::NGonList (degree ));
3135
3236 auto createOutView = [&](const ICPUPolygonGeometry::SDataView& inView) -> ICPUPolygonGeometry::SDataView
33- {
34- if (!inView) return {};
35- auto buffer = ICPUBuffer::create ({ inGeo->getPrimitiveCount () * indexing->degree () * inView.composed .stride , inView.src .buffer ->getUsageFlags () });
37+ {
38+ if (!inView)
39+ return {};
40+ auto buffer = ICPUBuffer::create ({ outIndexCount*inView.composed .stride , inView.src .buffer ->getUsageFlags () });
3641 return {
37- .composed = inView.composed ,
38- .src = {.offset = 0 , .size = buffer->getSize (), .buffer = std::move (buffer)}
42+ .composed = inView.composed ,
43+ .src = {.offset = 0 , .size = buffer->getSize (), .buffer = std::move (buffer)}
3944 };
40- };
45+ };
4146
4247 const auto inIndexView = inGeo->getIndexView ();
4348 auto outIndexView = createOutView (inIndexView);
4449 auto indexBuffer = outIndexView.src .buffer ;
4550 const auto indexSize = inIndexView.composed .stride ;
46- std::byte* outIndexes = reinterpret_cast <std::byte*>(outIndexView.getPointer ());
51+ std::byte* outIndices = reinterpret_cast <std::byte*>(outIndexView.getPointer ());
4752 outGeo->setIndexView ({});
4853
4954 const auto inVertexView = inGeo->getPositionView ();
5055 auto outVertexView = createOutView (inVertexView);
5156 auto vertexBuffer = outVertexView.src .buffer ;
5257 const auto vertexSize = inVertexView.composed .stride ;
53- const std::byte* inVertexes = reinterpret_cast <const std::byte*>(inVertexView.getPointer ());
54- std::byte* const outVertexes = reinterpret_cast <std::byte*>(vertexBuffer->getPointer ());
58+ const std::byte* inVertices = reinterpret_cast <const std::byte*>(inVertexView.getPointer ());
59+ std::byte* const outVertices = reinterpret_cast <std::byte*>(vertexBuffer->getPointer ());
5560 outGeo->setPositionView (std::move (outVertexView));
5661
5762 const auto inNormalView = inGeo->getNormalView ();
@@ -63,68 +68,67 @@ core::smart_refctd_ptr<ICPUPolygonGeometry> CPolygonGeometryManipulator::createU
6368 outGeometry->getJointWeightViews ()->resize (inGeo->getJointWeightViews ().size ());
6469 for (uint64_t jointView_i = 0u ; jointView_i < inGeo->getJointWeightViews ().size (); jointView_i++)
6570 {
66- auto & inJointWeightView = inGeo->getJointWeightViews ()[jointView_i];
67- auto & outJointWeightView = outGeometry->getJointWeightViews ()->operator [](jointView_i);
68- outJointWeightView.indices = createOutView (inJointWeightView.indices );
69- outJointWeightView.weights = createOutView (inJointWeightView.weights );
71+ auto & inJointWeightView = inGeo->getJointWeightViews ()[jointView_i];
72+ auto & outJointWeightView = outGeometry->getJointWeightViews ()->operator [](jointView_i);
73+ outJointWeightView.indices = createOutView (inJointWeightView.indices );
74+ outJointWeightView.weights = createOutView (inJointWeightView.weights );
7075 }
7176
7277 outGeometry->getAuxAttributeViews ()->resize (inGeo->getAuxAttributeViews ().size ());
7378 for (uint64_t auxView_i = 0u ; auxView_i < inGeo->getAuxAttributeViews ().size (); auxView_i++)
74- {
75- outGeo->getAuxAttributeViews ()->operator [](auxView_i) = createOutView (inGeo->getAuxAttributeViews ()[auxView_i]);
76- }
79+ outGeo->getAuxAttributeViews ()->operator [](auxView_i) = createOutView (inGeo->getAuxAttributeViews ()[auxView_i]);
7780
81+ std::array<uint32_t ,255 > indices;
7882 for (uint64_t prim_i = 0u ; prim_i < primCount; prim_i++)
7983 {
80- hlsl::uint32_t3 indexes;
81- IPolygonGeometryBase::IIndexingCallback::SContext<uint32_t > context{
82- .indexBuffer = indexView.getPointer (),
83- .indexSize = indexView.composed .stride ,
84- .beginPrimitive = prim_i,
85- .endPrimitive = prim_i + 1 ,
86- .out = &indexes
87- };
88- indexing->operator ()(context);
89- for (uint64_t primIndex_i = 0u ; primIndex_i < indexing->degree (); primIndex_i++)
90- {
91- const auto outIndex = prim_i * indexing->degree () + primIndex_i;
92- const auto inIndex = indexes[primIndex_i];
93- memcpy (outIndexes + outIndex * indexSize, &outIndex, indexSize);
94- memcpy (outVertexes + outIndex * vertexSize, inVertexes + inIndex * vertexSize, vertexSize);
95- if (inNormalView)
96- {
97- std::byte* const outNormals = reinterpret_cast <std::byte*>(outNormalBuffer->getPointer ());
98- const auto normalSize = inNormalView.composed .stride ;
99- memcpy (outNormals + outIndex * normalSize, inNormals + inIndex * normalSize, normalSize);
100- }
101-
102- for (uint64_t jointView_i = 0u ; jointView_i < inGeo->getJointWeightViews ().size (); jointView_i++)
103- {
104- auto & inView = inGeo->getJointWeightViews ()[jointView_i];
105- auto & outView = outGeometry->getJointWeightViews ()->operator [](jointView_i);
106-
107- const std::byte* const inJointIndices = reinterpret_cast <const std::byte*>(inView.indices .getPointer ());
108- const auto jointIndexSize = inView.indices .composed .stride ;
109- std::byte* const outJointIndices = reinterpret_cast <std::byte*>(outView.indices .getPointer ());
110- memcpy (outJointIndices + outIndex * jointIndexSize, inJointIndices + inIndex * jointIndexSize, jointIndexSize);
111-
112- const std::byte* const inWeights = reinterpret_cast <const std::byte*>(inView.weights .getPointer ());
113- const auto jointWeightSize = inView.weights .composed .stride ;
114- std::byte* const outWeights = reinterpret_cast <std::byte*>(outView.weights .getPointer ());
115- memcpy (outWeights + outIndex * jointWeightSize, outWeights + inIndex * jointWeightSize, jointWeightSize);
116- }
117-
118- for (uint64_t auxView_i = 0u ; auxView_i < inGeo->getAuxAttributeViews ().size (); auxView_i++)
84+ IPolygonGeometryBase::IIndexingCallback::SContext<uint32_t > context{
85+ .indexBuffer = indexView.getPointer (),
86+ .indexSize = indexView.composed .stride ,
87+ .beginPrimitive = prim_i,
88+ .endPrimitive = prim_i + 1 ,
89+ .out = indices.data ()
90+ };
91+ indexing->operator ()(context);
92+ for (uint8_t primIndex_i=0 ; primIndex_i<degree; primIndex_i++)
11993 {
120- auto & inView = inGeo->getAuxAttributeViews ()[auxView_i];
121- auto & outView = outGeometry->getAuxAttributeViews ()->operator [](auxView_i);
122- const auto attrSize = inView.composed .stride ;
123- const std::byte* const inAuxs = reinterpret_cast <const std::byte*>(inView.getPointer ());
124- std::byte* const outAuxs = reinterpret_cast <std::byte*>(outView.getPointer ());
125- memcpy (outAuxs + outIndex * attrSize, inAuxs + inIndex * attrSize, attrSize);
94+ const auto outIndex = prim_i * degree + primIndex_i;
95+ const auto inIndex = indices[primIndex_i];
96+ // TODO: these memcpys from view to view could really be DRY-ed and lambdified
97+ memcpy (outIndices + outIndex * indexSize, &outIndex, indexSize);
98+ memcpy (outVertices + outIndex * vertexSize, inVertices + inIndex * vertexSize, vertexSize);
99+ if (inNormalView)
100+ {
101+ std::byte* const outNormals = reinterpret_cast <std::byte*>(outNormalBuffer->getPointer ());
102+ const auto normalSize = inNormalView.composed .stride ;
103+ memcpy (outNormals + outIndex * normalSize, inNormals + inIndex * normalSize, normalSize);
104+ }
105+
106+ for (uint64_t jointView_i = 0u ; jointView_i < inGeo->getJointWeightViews ().size (); jointView_i++)
107+ {
108+ auto & inView = inGeo->getJointWeightViews ()[jointView_i];
109+ auto & outView = outGeometry->getJointWeightViews ()->operator [](jointView_i);
110+
111+ const std::byte* const inJointIndices = reinterpret_cast <const std::byte*>(inView.indices .getPointer ());
112+ const auto jointIndexSize = inView.indices .composed .stride ;
113+ std::byte* const outJointIndices = reinterpret_cast <std::byte*>(outView.indices .getPointer ());
114+ memcpy (outJointIndices + outIndex * jointIndexSize, inJointIndices + inIndex * jointIndexSize, jointIndexSize);
115+
116+ const std::byte* const inWeights = reinterpret_cast <const std::byte*>(inView.weights .getPointer ());
117+ const auto jointWeightSize = inView.weights .composed .stride ;
118+ std::byte* const outWeights = reinterpret_cast <std::byte*>(outView.weights .getPointer ());
119+ memcpy (outWeights + outIndex * jointWeightSize, outWeights + inIndex * jointWeightSize, jointWeightSize);
120+ }
121+
122+ for (uint64_t auxView_i = 0u ; auxView_i < inGeo->getAuxAttributeViews ().size (); auxView_i++)
123+ {
124+ auto & inView = inGeo->getAuxAttributeViews ()[auxView_i];
125+ auto & outView = outGeometry->getAuxAttributeViews ()->operator [](auxView_i);
126+ const auto attrSize = inView.composed .stride ;
127+ const std::byte* const inAuxs = reinterpret_cast <const std::byte*>(inView.getPointer ());
128+ std::byte* const outAuxs = reinterpret_cast <std::byte*>(outView.getPointer ());
129+ memcpy (outAuxs + outIndex * attrSize, inAuxs + inIndex * attrSize, attrSize);
130+ }
126131 }
127- }
128132 }
129133
130134 recomputeContentHashes (outGeo);
0 commit comments