Skip to content

Mesh loaders kevin #894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
287 changes: 4 additions & 283 deletions include/nbl/asset/ECommonEnums.h

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion include/nbl/asset/IBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ struct SBufferRange
inline operator SBufferRange<const BufferType>&() {return *reinterpret_cast<SBufferRange<const BufferType>*>(this);}
inline operator const SBufferRange<const BufferType>&() const {return *reinterpret_cast<const SBufferRange<const BufferType>*>(this);}

template<typename BT> requires std::is_same_v<std::remove_const_t<BT>,BufferType>
template<typename BT> requires (std::is_const_v<BT> && std::is_base_of_v<IBuffer,std::remove_const_t<BT>>)
inline operator SBufferBinding<BT>() const { return {.offset=offset,.buffer=buffer}; }

explicit inline operator bool() const {return isValid();}
Expand Down
40 changes: 21 additions & 19 deletions include/nbl/asset/IPolygonGeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,26 @@ class IPolygonGeometry : public IIndexableGeometry<BufferType>, public IPolygonG
// For User defined semantics
inline const core::vector<SDataView>& getAuxAttributeViews() const {return m_auxAttributeViews;}

inline E_INDEX_TYPE getIndexType() const
{
auto indexType = EIT_UNKNOWN;
// disallowed index format
if (base_t::m_indexView)
{
switch (base_t::m_indexView.composed.format)
{
case EF_R16_UINT:
indexType = EIT_16BIT;
break;
case EF_R32_UINT: [[fallthrough]];
indexType = EIT_32BIT;
break;
default:
break;
}
}
return indexType;
}

// Does not set the `transform` or `geometryFlags` fields, because it doesn't care about it.
// Also won't set second set of vertex data, opacity mipmaps, etc.
Expand All @@ -212,30 +232,12 @@ class IPolygonGeometry : public IIndexableGeometry<BufferType>, public IPolygonG
// must be a triangle list, but don't want to compare pointers
if (m_indexing && m_indexing->knownTopology()==EPT_TRIANGLE_LIST)// && m_indexing->degree() == TriangleList()->degree() && m_indexing->rate() == TriangleList->rate())
{
auto indexType = EIT_UNKNOWN;
// disallowed index format
if (base_t::m_indexView)
{
switch (base_t::m_indexView.composed.format)
{
case EF_R16_UINT:
indexType = EIT_16BIT;
break;
case EF_R32_UINT: [[fallthrough]];
indexType = EIT_32BIT;
break;
default:
break;
}
if (indexType==EIT_UNKNOWN)
return retval;
}
retval.vertexData[0] = base_t::m_positionView.src;
retval.indexData = base_t::m_indexView.src;
retval.maxVertex = base_t::m_positionView.getElementCount() - 1;
retval.vertexStride = base_t::m_positionView.composed.getStride();
retval.vertexFormat = base_t::m_positionView.composed.format;
retval.indexType = indexType;
retval.indexType = getIndexType();
}
return retval;
}
Expand Down
111 changes: 56 additions & 55 deletions include/nbl/asset/utils/CDirQuantCacheBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,25 +43,26 @@ class CDirQuantCacheBase

Vector8u3() : x(0u),y(0u),z(0u) {}
Vector8u3(const Vector8u3&) = default;
explicit Vector8u3(const core::vectorSIMDu32& val)
explicit Vector8u3(const hlsl::float32_t3& val)
{
operator=(val);
}

Vector8u3& operator=(const Vector8u3&) = default;
Vector8u3& operator=(const core::vectorSIMDu32& val)
Vector8u3& operator=(const hlsl::float32_t3& val)
{
x = val.x;
y = val.y;
z = val.z;
return *this;
}

inline core::vectorSIMDu32 getValue() const
hlsl::float32_t3 getValue() const
{
return core::vectorSIMDu32(x,y,z);
return { x, y, z };
}


private:
uint8_t x;
uint8_t y;
Expand All @@ -74,24 +75,24 @@ class CDirQuantCacheBase

Vector8u4() : x(0u),y(0u),z(0u),w(0u) {}
Vector8u4(const Vector8u4&) = default;
explicit Vector8u4(const core::vectorSIMDu32& val)
explicit Vector8u4(const hlsl::float32_t3& val)
{
operator=(val);
}

Vector8u4& operator=(const Vector8u4&) = default;
Vector8u4& operator=(const core::vectorSIMDu32& val)
Vector8u4& operator=(const hlsl::float32_t3& val)
{
x = val.x;
y = val.y;
z = val.z;
w = val.w;
w = 0;
return *this;
}

inline core::vectorSIMDu32 getValue() const
hlsl::float32_t3 getValue() const
{
return core::vectorSIMDu32(x,y,z,w);
return { x, y, z };
}

private:
Expand All @@ -108,16 +109,17 @@ class CDirQuantCacheBase

Vector1010102() : storage(0u) {}
Vector1010102(const Vector1010102&) = default;
explicit Vector1010102(const core::vectorSIMDu32& val)
explicit Vector1010102(const hlsl::float32_t3& val)
{
operator=(val);
}

Vector1010102& operator=(const Vector1010102&) = default;
Vector1010102& operator=(const core::vectorSIMDu32& val)
Vector1010102& operator=(const hlsl::float32_t3& val)
{
constexpr auto storageBits = quantizationBits+1u;
storage = val.x|(val.y<<storageBits)|(val.z<<(storageBits*2u));
constexpr auto storageBits = quantizationBits + 1u;
hlsl::uint32_t3 u32_val = { val.x, val.y, val.z };
storage = u32_val.x | (u32_val.y << storageBits) | (u32_val.z << (storageBits * 2u));
return *this;
}

Expand All @@ -130,13 +132,13 @@ class CDirQuantCacheBase
return storage==other.storage;
}

inline core::vectorSIMDu32 getValue() const
hlsl::float32_t3 getValue() const
{
constexpr auto storageBits = quantizationBits+1u;
const core::vectorSIMDu32 mask((0x1u<<storageBits)-1u);
return core::vectorSIMDu32(storage,storage>>storageBits,storage>>(storageBits*2u))&mask;
constexpr auto storageBits = quantizationBits + 1u;
const auto mask = (0x1u << storageBits) - 1u;
return { storage & mask, (storage >> storageBits) & mask, (storage >> (storageBits * 2)) & mask};
}

private:
uint32_t storage;
};
Expand All @@ -149,25 +151,25 @@ class CDirQuantCacheBase

Vector16u3() : x(0u),y(0u),z(0u) {}
Vector16u3(const Vector16u3&) = default;
explicit Vector16u3(const core::vectorSIMDu32& val)
explicit Vector16u3(const hlsl::float32_t3& val)
{
operator=(val);
}

Vector16u3& operator=(const Vector16u3&) = default;
Vector16u3& operator=(const core::vectorSIMDu32& val)
Vector16u3& operator=(const hlsl::float32_t3& val)
{
x = val.x;
y = val.y;
z = val.z;
return *this;
}

inline core::vectorSIMDu32 getValue() const
hlsl::float32_t3 getValue() const
{
return core::vectorSIMDu32(x,y,z);
return { x, y, z };
}

private:
uint16_t x;
uint16_t y;
Expand All @@ -180,26 +182,26 @@ class CDirQuantCacheBase

Vector16u4() : x(0u),y(0u),z(0u),w(0u) {}
Vector16u4(const Vector16u4&) = default;
explicit Vector16u4(const core::vectorSIMDu32& val)
explicit Vector16u4(const hlsl::float32_t3& val)
{
operator=(val);
}

Vector16u4& operator=(const Vector16u4&) = default;
Vector16u4& operator=(const core::vectorSIMDu32& val)
Vector16u4& operator=(const hlsl::float32_t3& val)
{
x = val.x;
y = val.y;
z = val.z;
w = val.w;
w = 0;
return *this;
}

inline core::vectorSIMDu32 getValue() const
hlsl::float32_t3 getValue() const
{
return core::vectorSIMDu32(x,y,z,w);
return { x, y, z };
}

private:
uint16_t x;
uint16_t y;
Expand Down Expand Up @@ -377,11 +379,11 @@ class CDirQuantCacheBase : public virtual core::IReferenceCounted, public impl::
std::tuple<cache_type_t<Formats>...> cache;

template<uint32_t dimensions, E_FORMAT CacheFormat>
value_type_t<CacheFormat> quantize(const core::vectorSIMDf& value)
value_type_t<CacheFormat> quantize(const hlsl::float32_t3& value)
{
const auto negativeMask = value < core::vectorSIMDf(0.0f);
const auto negativeMask = lessThan(value, hlsl::float32_t3(0.0f));

const core::vectorSIMDf absValue = abs(value);
const hlsl::float32_t3 absValue = abs(value);
const auto key = Key(absValue);

constexpr auto quantizationBits = quantization_bits_v<CacheFormat>;
Expand All @@ -393,32 +395,31 @@ class CDirQuantCacheBase : public virtual core::IReferenceCounted, public impl::
quantized = found->second;
else
{
const core::vectorSIMDf fit = findBestFit<dimensions,quantizationBits>(absValue);
const auto fit = findBestFit<dimensions,quantizationBits>(absValue);

quantized = core::vectorSIMDu32(core::abs(fit));
quantized = abs(fit);
insertIntoCache<CacheFormat>(key,quantized);
}
}

const core::vectorSIMDu32 xorflag((0x1u<<(quantizationBits+1u))-1u);
auto restoredAsVec = quantized.getValue()^core::mix(core::vectorSIMDu32(0u),xorflag,negativeMask);
restoredAsVec += core::mix(core::vectorSIMDu32(0u),core::vectorSIMDu32(1u),negativeMask);
return value_type_t<CacheFormat>(restoredAsVec&xorflag);
//return quantized.
const auto negativeMulVec = hlsl::float32_t3(negativeMask.x ? -1 : 1, negativeMask.y ? -1 : 1, negativeMask.z ? -1 : 1);
return value_type_t<CacheFormat>(negativeMulVec * quantized.getValue());
}

template<uint32_t dimensions, uint32_t quantizationBits>
static inline core::vectorSIMDf findBestFit(const core::vectorSIMDf& value)
static inline hlsl::float32_t3 findBestFit(const hlsl::float32_t3& value)
{
static_assert(dimensions>1u,"No point");
static_assert(dimensions<=4u,"High Dimensions are Hard!");
// precise normalize
const auto vectorForDots = value.preciseDivision(length(value));

const auto vectorForDots = hlsl::normalize(value);

//
core::vectorSIMDf fittingVector;
core::vectorSIMDf floorOffset;
hlsl::float32_t3 fittingVector;
hlsl::float32_t3 floorOffset;
constexpr uint32_t cornerCount = (0x1u<<(dimensions-1u))-1u;
core::vectorSIMDf corners[cornerCount] = {};
hlsl::float32_t3 corners[cornerCount] = {};
{
uint32_t maxDirCompIndex = 0u;
for (auto i=1u; i<dimensions; i++)
Expand All @@ -430,9 +431,9 @@ class CDirQuantCacheBase : public virtual core::IReferenceCounted, public impl::
if (maxDirectionComp < std::sqrtf(0.9998f / float(dimensions)))
{
_NBL_DEBUG_BREAK_IF(true);
return core::vectorSIMDf(0.f);
return hlsl::float32_t3(0.f);
}
fittingVector = value.preciseDivision(core::vectorSIMDf(maxDirectionComp));
fittingVector = value / maxDirectionComp;
floorOffset[maxDirCompIndex] = 0.499f;
const uint32_t localCorner[7][3] = {
{1,0,0},
Expand All @@ -452,12 +453,12 @@ class CDirQuantCacheBase : public virtual core::IReferenceCounted, public impl::
}
}

core::vectorSIMDf bestFit;
hlsl::float32_t3 bestFit;
float closestTo1 = -1.f;
auto evaluateFit = [&](const core::vectorSIMDf& newFit) -> void
auto evaluateFit = [&](const hlsl::float32_t3& newFit) -> void
{
auto newFitLen = core::length(newFit);
const float dp = core::dot<core::vectorSIMDf>(newFit,vectorForDots).preciseDivision(newFitLen)[0];
auto newFitLen = length(newFit);
const float dp = hlsl::dot(newFit,vectorForDots) / (newFitLen);
if (dp > closestTo1)
{
closestTo1 = dp;
Expand All @@ -466,18 +467,18 @@ class CDirQuantCacheBase : public virtual core::IReferenceCounted, public impl::
};

constexpr uint32_t cubeHalfSize = (0x1u << quantizationBits) - 1u;
const core::vectorSIMDf cubeHalfSizeND = core::vectorSIMDf(cubeHalfSize);
const hlsl::float32_t3 cubeHalfSizeND = hlsl::float32_t3(cubeHalfSize);
for (uint32_t n=cubeHalfSize; n>0u; n--)
{
//we'd use float addition in the interest of speed, to increment the loop
//but adding a small number to a large one loses precision, so multiplication preferrable
core::vectorSIMDf bottomFit = core::floor(fittingVector*float(n)+floorOffset);
if ((bottomFit<=cubeHalfSizeND).all())
const auto bottomFit = floor(fittingVector * float(n) + floorOffset);
if (hlsl::all(glm::lessThanEqual(bottomFit, cubeHalfSizeND)))
evaluateFit(bottomFit);
for (auto i=0u; i<cornerCount; i++)
for (auto i = 0u; i < cornerCount; i++)
{
auto bottomFitTmp = bottomFit+corners[i];
if ((bottomFitTmp<=cubeHalfSizeND).all())
if (hlsl::all(glm::lessThanEqual(bottomFitTmp, cubeHalfSizeND)))
evaluateFit(bottomFitTmp);
}
}
Expand Down
9 changes: 3 additions & 6 deletions include/nbl/asset/utils/CGeometryCreator.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,10 @@ class NBL_API2 CGeometryCreator final : public core::IReferenceCounted
\param colorCone color of the cone
\return Generated mesh.
*/
core::smart_refctd_ptr<ICPUPolygonGeometry> createArrow(const uint32_t tesselationCylinder = 4,
core::vector<core::smart_refctd_ptr<ICPUPolygonGeometry>> createArrow(const uint32_t tesselationCylinder = 4,
const uint32_t tesselationCone = 8, const float height = 1.f,
const float cylinderHeight = 0.6f, const float widthCylinder = 0.05f,
const float widthCone = 0.3f, const video::SColor colorCylinder = 0xFFFFFFFF,
const video::SColor colorCone = 0xFFFFFFFF) const;
const float widthCone = 0.3f) const;


//! Create a sphere mesh.
Expand All @@ -87,7 +86,7 @@ class NBL_API2 CGeometryCreator final : public core::IReferenceCounted
*/
core::smart_refctd_ptr<ICPUPolygonGeometry> createCylinder(float radius, float length,
uint32_t tesselation,
const video::SColor& color=video::SColor(0xffffffff), CQuantNormalCache* const quantNormalCacheOverride=nullptr) const;
CQuantNormalCache* const quantNormalCacheOverride=nullptr) const;

//! Create a cone mesh.
/**
Expand All @@ -100,8 +99,6 @@ class NBL_API2 CGeometryCreator final : public core::IReferenceCounted
\return Generated mesh.
*/
core::smart_refctd_ptr<ICPUPolygonGeometry> createCone(float radius, float length, uint32_t tesselation,
const video::SColor& colorTop=video::SColor(0xffffffff),
const video::SColor& colorBottom=video::SColor(0xffffffff),
float oblique=0.f, CQuantNormalCache* const quantNormalCacheOverride=nullptr) const;

core::smart_refctd_ptr<ICPUPolygonGeometry> createRectangle(const hlsl::float32_t2 size={0.5f,0.5f}) const;
Expand Down
5 changes: 2 additions & 3 deletions include/nbl/asset/utils/CQuantNormalCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace impl

struct VectorUV
{
inline VectorUV(const core::vectorSIMDf& absNormal)
inline VectorUV(const hlsl::float32_t3& absNormal)
{
const float rcpManhattanNorm = 1.f / (absNormal.x + absNormal.y + absNormal.z);
u = absNormal.x * rcpManhattanNorm;
Expand Down Expand Up @@ -56,9 +56,8 @@ class CQuantNormalCache : public CDirQuantCacheBase<impl::VectorUV,impl::QuantNo

public:
template<E_FORMAT CacheFormat>
value_type_t<CacheFormat> quantize(core::vectorSIMDf normal)
value_type_t<CacheFormat> quantize(hlsl::float32_t3 normal)
{
normal.makeSafe3D();
return Base::quantize<3u,CacheFormat>(normal);
}
};
Expand Down
Loading
Loading