Skip to content

Commit 8ed377a

Browse files
author
devsh
committed
implement rectangle and disk
1 parent 16b7c53 commit 8ed377a

File tree

3 files changed

+190
-93
lines changed

3 files changed

+190
-93
lines changed

include/nbl/asset/utils/CGeometryCreator.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ class NBL_API2 CGeometryCreator final : public core::IReferenceCounted
106106

107107
core::smart_refctd_ptr<ICPUPolygonGeometry> createRectangle(const hlsl::float32_t2 size={0.5f,0.5f}) const;
108108

109-
core::smart_refctd_ptr<ICPUPolygonGeometry> createDisk(float radius, uint32_t tesselation) const;
109+
core::smart_refctd_ptr<ICPUPolygonGeometry> createDisk(const float radius, const uint32_t tesselation) const;
110110

111111
//! Create a icosphere geometry
112112
/**

src/nbl/asset/utils/CGeometryCreator.cpp

Lines changed: 188 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55

66
#include "nbl/asset/utils/CGeometryCreator.h"
7+
#include "nbl/builtin/hlsl/tgmath.hlsl"
78

89
#include <iostream>
910
#include <iomanip>
@@ -99,7 +100,7 @@ core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createCube(const h
99100
uvs = reinterpret_cast<decltype(uvs)>(buff->getPointer());
100101
shapes::AABB<4,uint8_t> aabb;
101102
aabb.minVx = hlsl::vector<uint8_t,4>(0,0,0,0);
102-
aabb.maxVx = hlsl::vector<uint8_t,4>(127,127,0,0);
103+
aabb.maxVx = hlsl::vector<uint8_t,4>(255,255,0,0);
103104
retval->getAuxAttributeViews()->push_back({
104105
.composed = {
105106
.encodedDataRange = {.u8=aabb},
@@ -164,10 +165,10 @@ core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createCube(const h
164165
};
165166
const hlsl::vector<uint8_t, 2> uv[4] =
166167
{
167-
hlsl::vector<uint8_t,2>(0,127),
168-
hlsl::vector<uint8_t,2>(127,127),
169-
hlsl::vector<uint8_t,2>(127, 0),
170-
hlsl::vector<uint8_t,2>(0, 0)
168+
hlsl::vector<uint8_t,2>( 0,255),
169+
hlsl::vector<uint8_t,2>(255,255),
170+
hlsl::vector<uint8_t,2>(255, 0),
171+
hlsl::vector<uint8_t,2>( 0, 0)
171172
};
172173
for (size_t f=0ull; f<6ull; ++f)
173174
{
@@ -647,113 +648,209 @@ core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createCone(
647648

648649
return cone;
649650
}
651+
#endif
650652

651-
652-
core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createRectangleMesh(const core::vector2df_SIMD& _size) const
653+
core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createRectangle(const hlsl::float32_t2 size) const
653654
{
654-
return_type retval;
655-
constexpr size_t vertexSize = sizeof(CGeometryCreator::RectangleVertex);
656-
retval.inputParams = { 0b1111u,0b1u,{
657-
{0u,EF_R32G32B32_SFLOAT,offsetof(RectangleVertex,pos)},
658-
{0u,EF_R8G8B8A8_UNORM,offsetof(RectangleVertex,color)},
659-
{0u,EF_R8G8_USCALED,offsetof(RectangleVertex,uv)},
660-
{0u,EF_R32G32B32_SFLOAT,offsetof(RectangleVertex,normal)}
661-
},{vertexSize,SVertexInputBindingParams::EVIR_PER_VERTEX} };
662-
// Create indices
663-
retval.indexCount = 6;
664-
retval.indexType = asset::EIT_16BIT;
665-
uint16_t u[6];
655+
using namespace hlsl;
666656

667-
/*
668-
0---1
669-
| / |
670-
3---2
671-
*/
672-
u[0] = 0;
673-
u[1] = 3;
674-
u[2] = 1;
675-
u[3] = 1;
676-
u[4] = 3;
677-
u[5] = 2;
678-
679-
auto indices = asset::ICPUBuffer::create({ sizeof(u) });
680-
memcpy(indices->getPointer(), u, sizeof(u));
681-
indices->addUsageFlags(asset::IBuffer::EUF_INDEX_BUFFER_BIT);
682-
retval.indexBuffer = { 0ull, std::move(indices) };
657+
auto retval = core::make_smart_refctd_ptr<ICPUPolygonGeometry>();
658+
retval->setIndexing(IPolygonGeometryBase::TriangleList());
659+
660+
// Create indices
661+
{
662+
using index_t = uint16_t;
663+
/*
664+
0---1
665+
| / |
666+
3---2
667+
*/
668+
const index_t indices[] = {0,3,1,1,3,2};
669+
auto buffer = ICPUBuffer::create({
670+
{sizeof(indices),IBuffer::EUF_INDEX_BUFFER_BIT},
671+
const_cast<void*>((const void*)indices) // TODO: temporary till two different creation params (adopting needs non const void, copying needs const void only
672+
});
673+
shapes::AABB<4,index_t> aabb;
674+
aabb.minVx[0] = 0;
675+
aabb.maxVx[0] = 3;
676+
retval->setIndexView({
677+
.composed = {
678+
.encodedDataRange = {.u16=aabb},
679+
.stride = sizeof(index_t),
680+
.format = EF_R16_UINT,
681+
.rangeFormat = IGeometryBase::EAABBFormat::U16
682+
},
683+
.src = {.offset=0,.size=buffer->getSize(),.buffer=std::move(buffer)}
684+
});
685+
}
683686

684687
// Create vertices
685-
auto vertices = asset::ICPUBuffer::create({ 4 * vertexSize });
686-
RectangleVertex* ptr = (RectangleVertex*)vertices->getPointer();
687-
688-
ptr[0] = RectangleVertex(core::vector3df_SIMD(-1.0f, 1.0f, 0.0f) * _size, video::SColor(0xFFFFFFFFu),
689-
core::vector2du32_SIMD(0u, 1u), core::vector3df_SIMD(0.0f, 0.0f, 1.0f));
690-
ptr[1] = RectangleVertex(core::vector3df_SIMD( 1.0f, 1.0f, 0.0f) * _size, video::SColor(0xFFFFFFFFu),
691-
core::vector2du32_SIMD(1u, 1u), core::vector3df_SIMD(0.0f, 0.0f, 1.0f));
692-
ptr[2] = RectangleVertex(core::vector3df_SIMD( 1.0f, -1.0f, 0.0f) * _size, video::SColor(0xFFFFFFFFu),
693-
core::vector2du32_SIMD(1u, 0u), core::vector3df_SIMD(0.0f, 0.0f, 1.0f));
694-
ptr[3] = RectangleVertex(core::vector3df_SIMD(-1.0f, -1.0f, 0.0f) * _size, video::SColor(0xFFFFFFFFu),
695-
core::vector2du32_SIMD(0u, 0u), core::vector3df_SIMD(0.0f, 0.0f, 1.0f));
696-
697-
vertices->addUsageFlags(asset::IBuffer::EUF_VERTEX_BUFFER_BIT);
698-
retval.bindings[0] = {0ull, std::move(vertices)};
688+
{
689+
{
690+
const hlsl::float32_t2 positions[] = {
691+
hlsl::float32_t2(-size.x, size.y),
692+
hlsl::float32_t2( size.x, size.y),
693+
hlsl::float32_t2( size.x,-size.y),
694+
hlsl::float32_t2(-size.x,-size.y)
695+
};
696+
auto buff = ICPUBuffer::create({sizeof(positions),IBuffer::EUF_NONE});
697+
shapes::AABB<4,float32_t> aabb;
698+
aabb.minVx = float32_t4(-size,0.f,0.f);
699+
aabb.maxVx = float32_t4( size,0.f,0.f);
700+
retval->setPositionView({
701+
.composed = {
702+
.encodedDataRange = {.f32=aabb},
703+
.stride = sizeof(positions[0]),
704+
.format = EF_R32G32_SFLOAT,
705+
.rangeFormat = IGeometryBase::EAABBFormat::F32
706+
},
707+
.src = {.offset=0,.size=buff->getSize(),.buffer = std::move(buff)}
708+
});
709+
}
710+
{
711+
const hlsl::vector<int8_t,4> normals[] = {
712+
hlsl::vector<int8_t,4>(0,0,127,0),
713+
hlsl::vector<int8_t,4>(0,0,127,0),
714+
hlsl::vector<int8_t,4>(0,0,127,0),
715+
hlsl::vector<int8_t,4>(0,0,127,0)
716+
};
717+
auto buff = ICPUBuffer::create({sizeof(normals),IBuffer::EUF_NONE});
718+
shapes::AABB<4,int8_t> aabb;
719+
aabb.maxVx = hlsl::vector<int8_t,4>(0,0,127,0);
720+
aabb.minVx = -aabb.maxVx;
721+
retval->setNormalView({
722+
.composed = {
723+
.encodedDataRange = {.s8=aabb},
724+
.stride = sizeof(normals[0]),
725+
.format = EF_R8G8B8A8_SNORM,
726+
.rangeFormat = IGeometryBase::EAABBFormat::S8_NORM
727+
},
728+
.src = {.offset=0,.size=buff->getSize(),.buffer=std::move(buff)}
729+
});
730+
}
731+
{
732+
const hlsl::vector<uint8_t,2> uvs[] = {
733+
hlsl::vector<uint8_t,2>( 0,255),
734+
hlsl::vector<uint8_t,2>(255,255),
735+
hlsl::vector<uint8_t,2>(255, 0),
736+
hlsl::vector<uint8_t,2>( 0, 0)
737+
};
738+
auto buff = ICPUBuffer::create({sizeof(uvs),IBuffer::EUF_NONE});
739+
shapes::AABB<4,uint8_t> aabb;
740+
aabb.minVx = hlsl::vector<uint8_t,4>(0,0,0,0);
741+
aabb.maxVx = hlsl::vector<uint8_t,4>(255,255,0,0);
742+
retval->getAuxAttributeViews()->push_back({
743+
.composed = {
744+
.encodedDataRange = {.u8=aabb},
745+
.stride = sizeof(uvs[0]),
746+
.format = EF_R8G8_UNORM,
747+
.rangeFormat = IGeometryBase::EAABBFormat::U8_NORM
748+
},
749+
.src = {.offset=0,.size=buff->getSize(),.buffer=std::move(buff)}
750+
});
751+
}
752+
}
699753

700754
return retval;
701755
}
702756

703-
core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createDiskMesh(float radius, uint32_t tesselation) const
757+
core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createDisk(const float radius, const uint32_t tesselation) const
704758
{
705-
return_type retval;
706-
constexpr size_t vertexSize = sizeof(CGeometryCreator::DiskVertex);
707-
708-
retval.inputParams = { 0b1111u,0b1u,{
709-
{0u,EF_R32G32B32_SFLOAT,offsetof(DiskVertex,pos)},
710-
{0u,EF_R8G8B8A8_UNORM,offsetof(DiskVertex,color)},
711-
{0u,EF_R8G8_USCALED,offsetof(DiskVertex,uv)},
712-
{0u,EF_R32G32B32_SFLOAT,offsetof(DiskVertex,normal)}
713-
},{vertexSize,SVertexInputBindingParams::EVIR_PER_VERTEX} };
714-
retval.assemblyParams.primitiveType = EPT_TRIANGLE_FAN; // without indices
715-
retval.indexType = EIT_UNKNOWN;
716-
717-
const size_t vertexCount = 2u + tesselation;
718-
retval.indexCount = vertexCount;
759+
// need at least 120 external angles in the fan
760+
if (tesselation<2)
761+
return nullptr;
719762

720-
const float angle = 360.0f / static_cast<float>(tesselation);
721-
722-
auto vertices = asset::ICPUBuffer::create({ vertexCount * vertexSize });
723-
DiskVertex* ptr = (DiskVertex*)vertices->getPointer();
724-
725-
const core::vectorSIMDf v0(0.0f, radius, 0.0f, 1.0f);
726-
core::matrix3x4SIMD rotation;
727-
728-
//center
729-
ptr[0] = DiskVertex(core::vector3df_SIMD(0.0f), video::SColor(0xFFFFFFFFu),
730-
core::vector2du32_SIMD(0u, 1u), core::vector3df_SIMD(0.0f, 0.0f, 1.0f));
763+
using namespace hlsl;
731764

732-
//v0
733-
ptr[1] = DiskVertex(v0, video::SColor(0xFFFFFFFFu),
734-
core::vector2du32_SIMD(0u, 1u), core::vector3df_SIMD(0.0f, 0.0f, 1.0f));
765+
auto retval = core::make_smart_refctd_ptr<ICPUPolygonGeometry>();
766+
retval->setIndexing(IPolygonGeometryBase::TriangleFan());
735767

736-
//vn
737-
ptr[vertexCount - 1] = ptr[1];
768+
// without index buffer
769+
const size_t vertexCount = 2u + tesselation;
738770

739-
//v1, v2, ..., vn-1
740-
for (int i = 2; i < vertexCount-1; i++)
771+
float32_t2* positions;
772+
// for now because no reliable RGB10A2 encode and scant support for 24-bit UTB formats
773+
hlsl::vector<int8_t,4>* normals;
774+
//
775+
constexpr uint16_t UnityUV = 0xffffu;
776+
uint16_t2* uvs;
741777
{
742-
core::vectorSIMDf vn;
743-
core::matrix3x4SIMD rotMatrix;
744-
rotMatrix.setRotation(core::quaternion(0.0f, 0.0f, core::radians((i-1)*angle)));
745-
rotMatrix.transformVect(vn, v0);
746-
747-
ptr[i] = DiskVertex(vn, video::SColor(0xFFFFFFFFu),
748-
core::vector2du32_SIMD(0u, 1u), core::vector3df_SIMD(0.0f, 0.0f, 1.0f));
778+
{
779+
constexpr auto AttrSize = sizeof(decltype(*positions));
780+
auto buff = ICPUBuffer::create({AttrSize*vertexCount,IBuffer::EUF_NONE});
781+
positions = reinterpret_cast<decltype(positions)>(buff->getPointer());
782+
shapes::AABB<4,float32_t> aabb;
783+
aabb.maxVx = float32_t4(radius,radius,0.f,0.f);
784+
aabb.minVx = -aabb.maxVx;
785+
retval->setPositionView({
786+
.composed = {
787+
.encodedDataRange = {.f32=aabb},
788+
.stride = AttrSize,
789+
.format = EF_R32G32_SFLOAT,
790+
.rangeFormat = IGeometryBase::EAABBFormat::F32
791+
},
792+
.src = {.offset=0,.size=buff->getSize(),.buffer = std::move(buff)}
793+
});
794+
}
795+
{
796+
constexpr auto AttrSize = sizeof(decltype(*normals));
797+
auto buff = ICPUBuffer::create({AttrSize*vertexCount,IBuffer::EUF_NONE});
798+
normals = reinterpret_cast<decltype(normals)>(buff->getPointer());
799+
shapes::AABB<4,int8_t> aabb;
800+
aabb.maxVx = hlsl::vector<int8_t,4>(0,0,127,0);
801+
aabb.minVx = -aabb.maxVx;
802+
retval->setNormalView({
803+
.composed = {
804+
.encodedDataRange = {.s8=aabb},
805+
.stride = AttrSize,
806+
.format = EF_R8G8B8A8_SNORM,
807+
.rangeFormat = IGeometryBase::EAABBFormat::S8_NORM
808+
},
809+
.src = {.offset=0,.size=buff->getSize(),.buffer=std::move(buff)}
810+
});
811+
}
812+
{
813+
constexpr auto AttrSize = sizeof(decltype(*uvs));
814+
auto buff = ICPUBuffer::create({AttrSize*vertexCount,IBuffer::EUF_NONE});
815+
uvs = reinterpret_cast<decltype(uvs)>(buff->getPointer());
816+
shapes::AABB<4,uint16_t> aabb;
817+
aabb.minVx = uint16_t4(0,0,0,0);
818+
aabb.maxVx = uint16_t4(UnityUV,UnityUV,0,0);
819+
retval->getAuxAttributeViews()->push_back({
820+
.composed = {
821+
.encodedDataRange = {.u16=aabb},
822+
.stride = AttrSize,
823+
.format = EF_R16G16_UNORM,
824+
.rangeFormat = IGeometryBase::EAABBFormat::U16_NORM
825+
},
826+
.src = {.offset=0,.size=buff->getSize(),.buffer=std::move(buff)}
827+
});
828+
}
749829
}
750830

751-
vertices->addUsageFlags(asset::IBuffer::EUF_VERTEX_BUFFER_BIT);
752-
retval.bindings[0] = {0ull, std::move(vertices)};
831+
// populate data
832+
{
833+
const float angle = 360.f / static_cast<float>(tesselation);
834+
// center
835+
*(positions++) = float32_t2(0.f,0.f);
836+
*(uvs++) = uint16_t2(0,UnityUV);
837+
// last
838+
positions[tesselation] = float32_t3(0.f,radius,0.f);
839+
uvs[tesselation] = uint16_t2(UnityUV,0);
840+
for (auto i=0; i<tesselation; i++)
841+
{
842+
const float t = float(i)/float(tesselation);
843+
const float rad = t * 2.f * hlsl::numbers::pi<float>;
844+
*(positions++) = float32_t3(hlsl::sin(rad),hlsl::cos(rad),0.f);
845+
*(uvs++) = uint16_t2(t*UnityUV+0.5f,0);
846+
}
847+
}
848+
std::fill_n(normals,vertexCount,hlsl::vector<int8_t,4>(0,0,127,0));
753849

754850
return retval;
755851
}
756852

853+
#if 0
757854
/*
758855
Helpful Icosphere class implementation used to compute
759856
and create icopshere's vertices and indecies.

0 commit comments

Comments
 (0)