Skip to content

Commit 95809fa

Browse files
author
devsh
committed
untested version of CSerializedLoader.cpp
1 parent 11a2b9f commit 95809fa

File tree

1 file changed

+50
-135
lines changed

1 file changed

+50
-135
lines changed

src/nbl/ext/MitsubaLoader/CSerializedLoader.cpp

Lines changed: 50 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ asset::SAssetBundle CSerializedLoader::loadAsset(system::IFile* _file, const ass
107107

108108
constexpr size_t CHUNK = 256<<10;
109109
using page_t = Page<>;
110-
core::vector<page_t> decompressed(CHUNK/sizeof(page_t));
110+
auto decompressedResource = make_smart_refctd_ptr<adoption_memory_resource<core::vector<page_t>>>(core::vector<page_t>(CHUNK/sizeof(page_t)));
111+
auto& decompressed = decompressedResource->getBacker();
111112
for (uint32_t i=0; i<ctx.meshCount; i++)
112113
{
113114
auto localSize = ctx.meshOffsets->operator[](i+ctx.meshCount);
@@ -215,159 +216,73 @@ asset::SAssetBundle CSerializedLoader::loadAsset(system::IFile* _file, const ass
215216
if (ptr+totalDataSize > streamEnd)
216217
continue;
217218
}
219+
auto readIntoView = [&ptr]<typename OutVectorT>(IGeometry<ICPUBuffer>::SDataView& view, const auto& input)->void
220+
{
221+
const auto* const basePtr = reinterpret_cast<const std::decay_t<decltype(input)>*>(ptr);
222+
const auto vertexIx = std::distance(basePtr,&input);
223+
*reinterpret_cast<OutVectorT*>(view.getPointer(vertexIx)) = input;
224+
};
218225

219226
auto geo = make_smart_refctd_ptr<ICPUPolygonGeometry>();
220227
geo->setIndexing(IPolygonGeometryBase::TriangleList());
221228

222-
// TODO: adopted view
223-
auto indexbuf = ICPUBuffer::create({ indexDataSize });
224-
const uint32_t posAttrSize = typeSize*3u;
225-
// TODO: can adopt mapped file to avoid moving data in RAM
226-
auto posbuf = ICPUBuffer::create({ vertexCount*posAttrSize });
227-
228-
// cannot adopt mapped file, because these can be different formats (64bit not needed no matter what)
229+
{
230+
const auto alignment = 0x1ull<<hlsl::findLSB(ptrdiff_t(ptr));
231+
auto view = createView<true>(sourceIsDoubles ? EF_R64G64B64_SFLOAT:EF_R32G32B32_SFLOAT,vertexCount,ptr,smart_refctd_ptr(decompressedResource),alignment);
232+
ptr += view.src.actualSize();
233+
geo->setPositionView(std::move(view));
234+
}
235+
// cannot adopt decompressed memory, because these can be different formats (64bit not needed no matter what)
229236
// we let everyone outside compress our vertex attributes as they please
230237
using normal_t = hlsl::float32_t3;
231238
if (requiresNormals)
232-
geo->setNormalView(createView(EF_R32G32B32_SFLOAT,vertexCount));
239+
{
240+
if (!flags.hasFlags(MF_FACE_NORMALS))
241+
{
242+
auto view = createView(EF_R32G32B32_SFLOAT,vertexCount);
243+
auto readNormal = [&readIntoView,&view](const auto& input)->void{readIntoView.template operator()<normal_t>(view,input);};
244+
if (sourceIsDoubles)
245+
std::for_each_n(core::execution::seq,reinterpret_cast<const hlsl::float64_t3*>(ptr),vertexCount,readNormal);
246+
else
247+
std::for_each_n(core::execution::seq,reinterpret_cast<const hlsl::float32_t3*>(ptr),vertexCount,readNormal);
248+
geo->setNormalView(std::move(view));
249+
}
250+
ptr += vertexCount*typeSize*3;
251+
}
233252
// TODO: name the attributes!
234253
auto* const auxViews = geo->getAuxAttributeViews();
235254
// do not EVER get tempted by using half floats for UVs, T-junction meshes will f-u-^
236255
using uv_t = hlsl::float32_t2;
237256
if (hasUVs)
238-
auxViews->push_back(createView(EF_R32G32B32_SFLOAT,vertexCount));
239-
using color_t = hlsl::float16_t4;
240-
if (hasColors)
241-
auxViews->push_back(createView(EF_R32G32B32_SFLOAT,vertexCount));
242-
#if 0
243-
void* posPtr = posbuf->getPointer();
244-
auto* normalPtr = !normalbuf ? nullptr:reinterpret_cast<CQuantNormalCache::value_type_t<EF_A2B10G10R10_SNORM_PACK32>*>(normalbuf->getPointer());
245-
unaligned_vec2* uvPtr = !uvbuf ? nullptr:reinterpret_cast<unaligned_vec2*>(uvbuf->getPointer());
246-
uint32_t* colorPtr = !colorbuf ? nullptr:reinterpret_cast<uint32_t*>(colorbuf->getPointer());
247-
248-
249-
enableAttribute(POSITION_ATTRIBUTE,sourceIsDoubles ? EF_R64G64B64_SFLOAT:EF_R32G32B32_SFLOAT,posbuf);
250-
{
251-
core::aabbox3df aabb;
252-
auto readPositions = [&aabb,ptr,posPtr](const auto& pos) -> void
253257
{
254-
size_t vertexIx = std::distance(reinterpret_cast<decltype(&pos)>(ptr),&pos);
255-
const auto* coords = pos.pointer;
256-
if (vertexIx)
257-
aabb.addInternalPoint(coords[0],coords[1],coords[2]);
258+
auto view = createView(EF_R32G32_SFLOAT,vertexCount);
259+
auto readUV = [&readIntoView,&view](const auto& input)->void{readIntoView.template operator()<uv_t>(view,input);};
260+
if (sourceIsDoubles)
261+
std::for_each_n(core::execution::seq,reinterpret_cast<const hlsl::float64_t2*>(ptr),vertexCount,readUV);
258262
else
259-
aabb.reset(coords[0],coords[1],coords[2]);
260-
reinterpret_cast<std::remove_const_t<std::remove_reference_t<decltype(pos)>>*>(posPtr)[vertexIx] = pos;
261-
};
262-
if (sourceIsDoubles)
263-
{
264-
auto*& typedPtr = reinterpret_cast<unaligned_dvec3*&>(ptr);
265-
std::for_each_n(core::execution::seq,typedPtr,vertexCount,readPositions);
266-
typedPtr += vertexCount;
267-
}
268-
else
269-
{
270-
auto*& typedPtr = reinterpret_cast<unaligned_vec3*&>(ptr);
271-
std::for_each_n(core::execution::seq,typedPtr,vertexCount,readPositions);
272-
typedPtr += vertexCount;
273-
}
274-
meshBuffer->setBoundingBox(aabb);
275-
}
276-
if (requiresNormals)
277-
{
278-
enableAttribute(NORMAL_ATTRIBUTE,EF_A2B10G10R10_SNORM_PACK32,normalbuf);
279-
auto readNormals = [quantNormalCache,ptr,normalPtr](const auto& nml) -> void
280-
{
281-
size_t vertexIx = std::distance(reinterpret_cast<decltype(&nml)>(ptr),&nml);
282-
core::vectorSIMDf simdNormal(nml.pointer[0],nml.pointer[1],nml.pointer[2]);
283-
normalPtr[vertexIx] = quantNormalCache->quantize<EF_A2B10G10R10_SNORM_PACK32>(simdNormal);
284-
};
285-
const bool read = flags&MF_PER_VERTEX_NORMALS;
286-
if (sourceIsDoubles)
287-
{
288-
auto*& typedPtr = reinterpret_cast<unaligned_dvec3*&>(ptr);
289-
if (read)
290-
std::for_each_n(core::execution::seq,typedPtr,vertexCount,readNormals);
291-
typedPtr += vertexCount;
292-
}
293-
else
294-
{
295-
auto*& typedPtr = reinterpret_cast<unaligned_vec3*&>(ptr);
296-
if (read)
297-
std::for_each_n(core::execution::seq,typedPtr,vertexCount,readNormals);
298-
typedPtr += vertexCount;
299-
}
300-
meshBuffer->setNormalAttributeIx(NORMAL_ATTRIBUTE);
301-
}
302-
if (hasUVs)
303-
{
304-
enableAttribute(UV_ATTRIBUTE,EF_R32G32_SFLOAT,uvbuf);
305-
auto readUVs = [ptr,uvPtr](const auto& uv) -> void
306-
{
307-
size_t vertexIx = std::distance(reinterpret_cast<decltype(&uv)>(ptr),&uv);
308-
for (auto k=0u; k<2u; k++)
309-
uvPtr[vertexIx].pointer[k] = uv.pointer[k];
310-
};
311-
if (sourceIsDoubles)
312-
{
313-
auto*& typedPtr = reinterpret_cast<unaligned_dvec2*&>(ptr);
314-
std::for_each_n(core::execution::seq,typedPtr,vertexCount,readUVs);
315-
typedPtr += vertexCount;
263+
std::for_each_n(core::execution::seq,reinterpret_cast<const hlsl::float32_t2*>(ptr),vertexCount,readUV);
264+
ptr += vertexCount*typeSize*2;
265+
auxViews->push_back(std::move(view));
316266
}
317-
else
318-
{
319-
auto*& typedPtr = reinterpret_cast<unaligned_vec2*&>(ptr);
320-
std::for_each_n(core::execution::seq,typedPtr,vertexCount,readUVs);
321-
typedPtr += vertexCount;
322-
}
323-
}
324-
if (hasColors)
325-
{
326-
enableAttribute(COLOR_ATTRIBUTE,EF_B10G11R11_UFLOAT_PACK32,colorbuf);
327-
auto readColors = [ptr,colorPtr](const auto& color) -> void
328-
{
329-
size_t vertexIx = std::distance(reinterpret_cast<decltype(&color)>(ptr),&color);
330-
const double colors[3] = {color.pointer[0],color.pointer[1],color.pointer[2]};
331-
encodePixels<EF_B10G11R11_UFLOAT_PACK32,double>(colorPtr+vertexIx,colors);
332-
};
333-
if (sourceIsDoubles)
267+
using color_t = hlsl::float16_t4;
268+
if (hasColors)
334269
{
335-
auto*& typedPtr = reinterpret_cast<unaligned_dvec3*&>(ptr);
336-
std::for_each_n(core::execution::seq,typedPtr,vertexCount,readColors);
337-
typedPtr += vertexCount;
270+
auto view = createView(EF_R16G16B16A16_SFLOAT,vertexCount);
271+
auto readColor = [&readIntoView,&view](const auto& input)->void{readIntoView.template operator()<color_t>(view,input);};
272+
if (sourceIsDoubles)
273+
std::for_each_n(core::execution::seq,reinterpret_cast<const hlsl::float64_t4*>(ptr),vertexCount,readColor);
274+
else
275+
std::for_each_n(core::execution::seq,reinterpret_cast<const hlsl::float32_t4*>(ptr),vertexCount,readColor);
276+
ptr += vertexCount*typeSize*4;
277+
auxViews->push_back(std::move(view));
338278
}
339-
else
279+
340280
{
341-
auto*& typedPtr = reinterpret_cast<unaligned_vec3*&>(ptr);
342-
std::for_each_n(core::execution::seq,typedPtr,vertexCount,readColors);
343-
typedPtr += vertexCount;
281+
const auto alignment = 0x1ull<<hlsl::findLSB(ptrdiff_t(ptr));
282+
auto view = createView<true>(EF_R32_UINT,triangleCount*3,ptr,smart_refctd_ptr(decompressedResource),alignment);
283+
ptr += view.src.actualSize();
284+
geo->setIndexView(std::move(view));
344285
}
345-
}
346-
347-
meshBuffer->setIndexBufferBinding({0u,indexbuf});
348-
meshBuffer->setIndexCount(triangleCount * 3u);
349-
meshBuffer->setIndexType(EIT_32BIT);
350-
351-
// read indices and possibly create per-face normals
352-
auto readIndices = [&]() -> bool
353-
{
354-
uint32_t* indexPtr = reinterpret_cast<uint32_t*>(indexbuf->getPointer());
355-
for (uint64_t j=0ull; j<triangleCount; j++)
356-
{
357-
uint32_t* triangleIndices = indexPtr;
358-
for (uint64_t k=0ull; k<3ull; k++)
359-
{
360-
triangleIndices[k] = *(reinterpret_cast<uint32_t*&>(ptr)++);
361-
if (triangleIndices[k] >= static_cast<uint32_t>(vertexCount))
362-
return false;
363-
}
364-
indexPtr += 3u;
365-
}
366-
return true;
367-
};
368-
if (!readIndices())
369-
continue;
370-
#endif
371286

372287
meta->placeMeta(geoms.size(),geo.get(),{std::string(stringPtr,stringLen),i});
373288
geoms.push_back(std::move(geo));

0 commit comments

Comments
 (0)