Skip to content

Commit 30eff84

Browse files
author
devsh
committed
implement cube geometry creation
1 parent 2aa2395 commit 30eff84

File tree

4 files changed

+145
-94
lines changed

4 files changed

+145
-94
lines changed

include/nbl/asset/ICPUBuffer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ namespace nbl::asset
2525
class ICPUBuffer final : public asset::IBuffer, public IPreHashed
2626
{
2727
public:
28+
// TODO: template to make `data` a `const void*` vs `void*`
2829
struct SCreationParams : asset::IBuffer::SCreationParams
2930
{
3031
void* data = nullptr;

include/nbl/asset/utils/CGeometryCreator.h

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,25 @@ namespace nbl::asset
1717

1818
//! Helper class for creating geometry on the fly.
1919
/** You can get an instance of this class through ISceneManager::getGeometryCreator() */
20-
class CGeometryCreator final : public core::IReferenceCounted
20+
class NBL_API2 CGeometryCreator final : public core::IReferenceCounted
2121
{
22-
core::smart_refctd_ptr<CQuantNormalCache> m_normalCache;
23-
core::smart_refctd_ptr<CQuantQuaternionCache> m_quaternionCache;
24-
2522
public:
26-
inline CGeometryCreator(core::smart_refctd_ptr<CQuantNormalCache>&& _normalCache, core::smart_refctd_ptr<CQuantQuaternionCache>&& _quaternionCache)
27-
: m_normalCache(std::move(_normalCache)), m_quaternionCache(std::move(_quaternionCache))
23+
struct SCreationParams
24+
{
25+
core::smart_refctd_ptr<CQuantNormalCache> normalCache = nullptr;
26+
core::smart_refctd_ptr<CQuantQuaternionCache> quaternionCache = nullptr;
27+
};
28+
inline CGeometryCreator(SCreationParams&& params={}) : m_params(std::move(params))
2829
{
29-
assert(m_normalCache && m_quaternionCache);
30+
if (!m_params.normalCache)
31+
m_params.normalCache = core::make_smart_refctd_ptr<CQuantNormalCache>();
32+
if (!m_params.quaternionCache)
33+
m_params.quaternionCache = core::make_smart_refctd_ptr<CQuantQuaternionCache>();
3034
}
3135

36+
//
37+
const SCreationParams& getCreationParams() const {return m_params;}
38+
3239
//! Creates a simple cube mesh.
3340
/**
3441
\param size Dimensions of the cube.
@@ -110,6 +117,8 @@ class CGeometryCreator final : public core::IReferenceCounted
110117

111118
core::smart_refctd_ptr<ICPUPolygonGeometry> createIcoSphere(float radius=1.f, uint32_t subdivision=1, bool smooth=false) const;
112119

120+
private:
121+
SCreationParams m_params;
113122
};
114123

115124
} // end namespace nbl::asset

src/nbl/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ set(NBL_ASSET_SOURCES
156156
${NBL_ROOT_PATH}/src/nbl/asset/IAssetManager.cpp
157157
${NBL_ROOT_PATH}/src/nbl/asset/ICPUDescriptorSet.cpp
158158
${NBL_ROOT_PATH}/src/nbl/asset/ICPUImage.cpp
159+
${NBL_ROOT_PATH}/src/nbl/asset/ICPUPolygonGeometry.cpp
159160
${NBL_ROOT_PATH}/src/nbl/asset/interchange/IAssetWriter.cpp
160161
${NBL_ROOT_PATH}/src/nbl/asset/interchange/IAssetLoader.cpp
161162

src/nbl/asset/utils/CGeometryCreator.cpp

Lines changed: 127 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,20 @@
1414
namespace nbl::asset
1515
{
1616

17-
#if 0
1817
core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createCube(const hlsl::float32_t3 size) const
1918
{
2019
using namespace hlsl;
2120

22-
23-
// {0u,EF_R32G32B32_SFLOAT,offsetof(CubeVertex,pos)},
24-
// {0u,EF_R8G8B8A8_UNORM,offsetof(CubeVertex,color)},
25-
// {0u,EF_R8G8_USCALED,offsetof(CubeVertex,uv)},
26-
// {0u,EF_R8G8B8_SSCALED,offsetof(CubeVertex,normal)}
21+
auto retval = core::make_smart_refctd_ptr<ICPUPolygonGeometry>();
22+
retval->setIndexing(IPolygonGeometryBase::TriangleList());
2723

2824
// Create indices
25+
using index_t = uint16_t;
2926
{
30-
retval.indexCount = 36u;
31-
auto indices = asset::ICPUBuffer::create({ sizeof(uint16_t)*retval.indexCount });
32-
indices->addUsageFlags(asset::IBuffer::EUF_INDEX_BUFFER_BIT);
33-
auto u = reinterpret_cast<uint16_t*>(indices->getPointer());
27+
constexpr auto IndexCount = 36u;
28+
constexpr auto bytesize = sizeof(index_t) * IndexCount;
29+
auto indices = ICPUBuffer::create({bytesize,IBuffer::EUF_INDEX_BUFFER_BIT});
30+
auto u = reinterpret_cast<index_t*>(indices->getPointer());
3431
for (uint32_t i=0u; i<6u; ++i)
3532
{
3633
u[i*6+0] = 4*i+0;
@@ -40,104 +37,147 @@ core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createCube(const h
4037
u[i*6+4] = 4*i+2;
4138
u[i*6+5] = 4*i+3;
4239
}
43-
retval.indexBuffer = {0ull,std::move(indices)};
40+
shapes::AABB<4,index_t> aabb;
41+
aabb.minVx[0] = 0;
42+
aabb.maxVx[0] = 23;
43+
retval->setIndexView({
44+
.composed = {
45+
.encodedDataRange = {.u16=aabb},
46+
.stride = sizeof(index_t),
47+
.format = EF_R16_UINT,
48+
.rangeFormat = IGeometryBase::EAABBFormat::U16
49+
},
50+
.src = {.offset=0,.size=bytesize,.buffer=std::move(indices)}
51+
});
4452
}
4553

46-
// Create vertices
47-
auto vertices = asset::ICPUBuffer::create({ 24u*vertexSize });
48-
vertices->addUsageFlags(IBuffer::EUF_VERTEX_BUFFER_BIT);
49-
CubeVertex* ptr = (CubeVertex*)vertices->getPointer();
50-
51-
const core::vector3d<int8_t> normals[6] =
54+
//
55+
const hlsl::float32_t3 pos[8] =
5256
{
53-
core::vector3d<int8_t>(0, 0, 1),
54-
core::vector3d<int8_t>(1, 0, 0),
55-
core::vector3d<int8_t>(0, 0, -1),
56-
core::vector3d<int8_t>(-1, 0, 0),
57-
core::vector3d<int8_t>(0, 1, 0),
58-
core::vector3d<int8_t>(0, -1, 0)
57+
hlsl::float32_t3(-0.5f,-0.5f, 0.5f) * size,
58+
hlsl::float32_t3(0.5f,-0.5f, 0.5f) * size,
59+
hlsl::float32_t3(0.5f, 0.5f, 0.5f) * size,
60+
hlsl::float32_t3(-0.5f, 0.5f, 0.5f) * size,
61+
hlsl::float32_t3(0.5f,-0.5f,-0.5f) * size,
62+
hlsl::float32_t3(-0.5f, 0.5f,-0.5f) * size,
63+
hlsl::float32_t3(-0.5f,-0.5f,-0.5f) * size,
64+
hlsl::float32_t3(0.5f, 0.5f,-0.5f) * size
5965
};
60-
const float32_t3 pos[8] =
66+
constexpr auto Dim = 4; // for now because no reliable RGB10A2 encode and scant support for 24-bit UTB formats
67+
const hlsl::vector<int8_t,Dim> norm[6] =
6168
{
62-
float32_t3(-0.5f,-0.5f, 0.5f)*size,
63-
float32_t3( 0.5f,-0.5f, 0.5f)*size,
64-
float32_t3( 0.5f, 0.5f, 0.5f)*size,
65-
float32_t3(-0.5f, 0.5f, 0.5f)*size,
66-
float32_t3( 0.5f,-0.5f,-0.5f)*size,
67-
float32_t3(-0.5f, 0.5f,-0.5f)*size,
68-
float32_t3(-0.5f,-0.5f,-0.5f)*size,
69-
float32_t3( 0.5f, 0.5f,-0.5f)*size
69+
hlsl::vector<int8_t,Dim>(0, 0, 127, 0),
70+
hlsl::vector<int8_t,Dim>(127, 0, 0, 0),
71+
hlsl::vector<int8_t,Dim>(0, 0,-127, 0),
72+
hlsl::vector<int8_t,Dim>(-127, 0, 0, 0),
73+
hlsl::vector<int8_t,Dim>(0, 127, 0, 0),
74+
hlsl::vector<int8_t,Dim>(0,-127, 0, 0)
7075
};
71-
const core::vector2d<uint8_t> uvs[4] =
76+
const hlsl::vector<uint8_t,2> uv[4] =
7277
{
73-
core::vector2d<uint8_t>(0, 1),
74-
core::vector2d<uint8_t>(1, 1),
75-
core::vector2d<uint8_t>(1, 0),
76-
core::vector2d<uint8_t>(0, 0)
78+
hlsl::vector<uint8_t,2>( 0,127),
79+
hlsl::vector<uint8_t,2>(127,127),
80+
hlsl::vector<uint8_t,2>(127, 0),
81+
hlsl::vector<uint8_t,2>( 0, 0)
7782
};
7883

84+
// Create vertex attributes with NONE usage because we have no clue how they'll be used
85+
hlsl::float32_t3* positions;
86+
hlsl::vector<int8_t,Dim>* normals;
87+
hlsl::vector<uint8_t,2>* uvs;
88+
{
89+
{
90+
auto buff = ICPUBuffer::create({sizeof(pos),IBuffer::EUF_NONE});
91+
positions = reinterpret_cast<decltype(positions)>(buff->getPointer());
92+
shapes::AABB<4,float32_t> aabb;
93+
aabb.maxVx = float32_t4(size*0.5f,0.f);
94+
aabb.minVx = -aabb.maxVx;
95+
retval->setPositionView({
96+
.composed = {
97+
.encodedDataRange = {.f32=aabb},
98+
.stride = sizeof(pos[0]),
99+
.format = EF_R32G32B32_SFLOAT,
100+
.rangeFormat = IGeometryBase::EAABBFormat::F32
101+
},
102+
.src = {.offset=0,.size=sizeof(pos),.buffer=std::move(buff)}
103+
});
104+
}
105+
{
106+
auto buff = ICPUBuffer::create({sizeof(norm),IBuffer::EUF_NONE});
107+
normals = reinterpret_cast<decltype(normals)>(buff->getPointer());
108+
shapes::AABB<4,int8_t> aabb;
109+
aabb.maxVx = hlsl::vector<int8_t,4>(127,127,127,0);
110+
aabb.minVx = -aabb.maxVx;
111+
retval->setPositionView({
112+
.composed = {
113+
.encodedDataRange = {.s8=aabb},
114+
.stride = sizeof(norm[0]),
115+
.format = EF_R8G8B8A8_SNORM,
116+
.rangeFormat = IGeometryBase::EAABBFormat::S8_NORM
117+
},
118+
.src = {.offset=0,.size=sizeof(norm),.buffer=std::move(buff)}
119+
});
120+
}
121+
{
122+
auto buff = ICPUBuffer::create({sizeof(uv),IBuffer::EUF_NONE});
123+
uvs = reinterpret_cast<decltype(uvs)>(buff->getPointer());
124+
shapes::AABB<4,uint8_t> aabb;
125+
aabb.minVx = hlsl::vector<uint8_t,4>(0,0,0,0);
126+
aabb.maxVx = hlsl::vector<uint8_t,4>(127,127,0,0);
127+
retval->setPositionView({
128+
.composed = {
129+
.encodedDataRange = {.u8=aabb},
130+
.stride = sizeof(uv[0]),
131+
.format = EF_R8G8_UNORM,
132+
.rangeFormat = IGeometryBase::EAABBFormat::U8_NORM
133+
},
134+
.src = {.offset=0,.size=sizeof(uv),.buffer=std::move(buff)}
135+
});
136+
}
137+
}
138+
139+
//
140+
positions[0] = hlsl::float32_t3(pos[0][0], pos[0][1], pos[0][2]);
141+
positions[1] = hlsl::float32_t3(pos[1][0], pos[1][1], pos[1][2]);
142+
positions[2] = hlsl::float32_t3(pos[2][0], pos[2][1], pos[2][2]);
143+
positions[3] = hlsl::float32_t3(pos[3][0], pos[3][1], pos[3][2]);
144+
positions[4] = hlsl::float32_t3(pos[1][0], pos[1][1], pos[1][2]);
145+
positions[5] = hlsl::float32_t3(pos[4][0], pos[4][1], pos[4][2]);
146+
positions[6] = hlsl::float32_t3(pos[7][0], pos[7][1], pos[7][2]);
147+
positions[7] = hlsl::float32_t3(pos[2][0], pos[2][1], pos[2][2]);
148+
positions[8] = hlsl::float32_t3(pos[4][0], pos[4][1], pos[4][2]);
149+
positions[9] = hlsl::float32_t3(pos[6][0], pos[6][1], pos[6][2]);
150+
positions[10] = hlsl::float32_t3(pos[5][0], pos[5][1], pos[5][2]);
151+
positions[11] = hlsl::float32_t3(pos[7][0], pos[7][1], pos[7][2]);
152+
positions[12] = hlsl::float32_t3(pos[6][0], pos[6][1], pos[6][2]);
153+
positions[13] = hlsl::float32_t3(pos[0][0], pos[0][1], pos[0][2]);
154+
positions[14] = hlsl::float32_t3(pos[3][0], pos[3][1], pos[3][2]);
155+
positions[15] = hlsl::float32_t3(pos[5][0], pos[5][1], pos[5][2]);
156+
positions[16] = hlsl::float32_t3(pos[3][0], pos[3][1], pos[3][2]);
157+
positions[17] = hlsl::float32_t3(pos[2][0], pos[2][1], pos[2][2]);
158+
positions[18] = hlsl::float32_t3(pos[7][0], pos[7][1], pos[7][2]);
159+
positions[19] = hlsl::float32_t3(pos[5][0], pos[5][1], pos[5][2]);
160+
positions[20] = hlsl::float32_t3(pos[0][0], pos[0][1], pos[0][2]);
161+
positions[21] = hlsl::float32_t3(pos[6][0], pos[6][1], pos[6][2]);
162+
positions[22] = hlsl::float32_t3(pos[4][0], pos[4][1], pos[4][2]);
163+
positions[23] = hlsl::float32_t3(pos[1][0], pos[1][1], pos[1][2]);
164+
165+
//
79166
for (size_t f=0ull; f<6ull; ++f)
80167
{
81168
const size_t v = f*4ull;
82169

83170
for (size_t i=0ull; i<4ull; ++i)
84171
{
85-
const core::vector3d<int8_t>& n = normals[f];
86-
const core::vector2d<uint8_t>& uv = uvs[i];
87-
ptr[v+i].setColor(255, 255, 255, 255);
88-
ptr[v+i].setNormal(n.X, n.Y, n.Z);
89-
ptr[v+i].setUv(uv.X, uv.Y);
90-
}
91-
92-
switch (f)
93-
{
94-
case 0:
95-
ptr[v+0].setPos(pos[0].X, pos[0].Y, pos[0].Z);
96-
ptr[v+1].setPos(pos[1].X, pos[1].Y, pos[1].Z);
97-
ptr[v+2].setPos(pos[2].X, pos[2].Y, pos[2].Z);
98-
ptr[v+3].setPos(pos[3].X, pos[3].Y, pos[3].Z);
99-
break;
100-
case 1:
101-
ptr[v+0].setPos(pos[1].X, pos[1].Y, pos[1].Z);
102-
ptr[v+1].setPos(pos[4].X, pos[4].Y, pos[4].Z);
103-
ptr[v+2].setPos(pos[7].X, pos[7].Y, pos[7].Z);
104-
ptr[v+3].setPos(pos[2].X, pos[2].Y, pos[2].Z);
105-
break;
106-
case 2:
107-
ptr[v+0].setPos(pos[4].X, pos[4].Y, pos[4].Z);
108-
ptr[v+1].setPos(pos[6].X, pos[6].Y, pos[6].Z);
109-
ptr[v+2].setPos(pos[5].X, pos[5].Y, pos[5].Z);
110-
ptr[v+3].setPos(pos[7].X, pos[7].Y, pos[7].Z);
111-
break;
112-
case 3:
113-
ptr[v+0].setPos(pos[6].X, pos[6].Y, pos[6].Z);
114-
ptr[v+2].setPos(pos[3].X, pos[3].Y, pos[3].Z);
115-
ptr[v+1].setPos(pos[0].X, pos[0].Y, pos[0].Z);
116-
ptr[v+3].setPos(pos[5].X, pos[5].Y, pos[5].Z);
117-
break;
118-
case 4:
119-
ptr[v+0].setPos(pos[3].X, pos[3].Y, pos[3].Z);
120-
ptr[v+1].setPos(pos[2].X, pos[2].Y, pos[2].Z);
121-
ptr[v+2].setPos(pos[7].X, pos[7].Y, pos[7].Z);
122-
ptr[v+3].setPos(pos[5].X, pos[5].Y, pos[5].Z);
123-
break;
124-
case 5:
125-
ptr[v+0].setPos(pos[0].X, pos[0].Y, pos[0].Z);
126-
ptr[v+1].setPos(pos[6].X, pos[6].Y, pos[6].Z);
127-
ptr[v+2].setPos(pos[4].X, pos[4].Y, pos[4].Z);
128-
ptr[v+3].setPos(pos[1].X, pos[1].Y, pos[1].Z);
129-
break;
172+
normals[v+i] = norm[f];
173+
uvs[v+i] = uv[i];
130174
}
131175
}
132-
retval.bindings[0] = {0ull,std::move(vertices)};
133-
134-
// Recalculate bounding box
135-
retval.indexType = asset::EIT_16BIT;
136-
retval.bbox = core::aabbox3df(-size*0.5f,size*0.5f);
137176

138177
return retval;
139178
}
140179

180+
#if 0
141181

142182
/*
143183
a cylinder, a cone and a cross

0 commit comments

Comments
 (0)