5
5
6
6
#include " nbl/asset/utils/CGeometryCreator.h"
7
7
#include " nbl/builtin/hlsl/tgmath.hlsl"
8
+ #include " nbl/builtin/hlsl/math/linalg/transform.hlsl"
8
9
9
- #include < iostream>
10
- #include < iomanip>
11
10
#include < cmath>
12
11
#include < cstdint>
13
12
14
-
15
13
namespace nbl ::asset
16
14
{
17
15
@@ -159,6 +157,12 @@ static ICPUPolygonGeometry::SDataView createSnormNormalView(size_t normalCount,
159
157
};
160
158
}
161
159
160
+ static void encodeUv (hlsl::vector<uint16_t , 2 >* uvDst, hlsl::float32_t2 uvSrc)
161
+ {
162
+ uint32_t u32_uv = hlsl::packUnorm2x16 (uvSrc);
163
+ memcpy (uvDst, &u32_uv, sizeof (uint16_t ) * 2 );
164
+ }
165
+
162
166
core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createCube (const hlsl::float32_t3 size) const
163
167
{
164
168
using namespace hlsl ;
@@ -458,16 +462,15 @@ core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createSphere(float
458
462
// if (y==0)
459
463
// {
460
464
if (normal.y != -1 .0f && normal.y != 1 .0f )
461
- tu = static_cast <float >(acos (core::clamp (normal.x / sinay, -1.0 , 1.0 )) * 0.5 * numbers::inv_pi<float32_t >() );
465
+ tu = static_cast <float >(acos (core::clamp (normal.x / sinay, -1.0 , 1.0 )) * 0.5 * numbers::inv_pi<float32_t >);
462
466
if (normal.z < 0 .0f )
463
467
tu = 1 - tu;
464
468
// }
465
469
// else
466
470
// tu = ((float*)(tmpMem+(i-polyCountXPitch)*vertexSize))[4];
467
471
468
472
positions[vertex_i] = pos;
469
- float32_t2 f32_uv = { tu, static_cast <float >(ay * numbers::inv_pi<float32_t >()) };
470
- encodePixels<get_uv_format<uv_element_t >(), float >(uvs + vertex_i, f32_uv.data .data );
473
+ encodeUv (uvs + vertex_i, float32_t2 (tu, static_cast <float >(ay* numbers::inv_pi<float32_t >)));
471
474
memcpy (normals + vertex_i, &quantizedNormal, sizeof (quantizedNormal));
472
475
473
476
vertex_i++;
@@ -476,7 +479,7 @@ core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createSphere(float
476
479
// This is the doubled vertex on the initial position
477
480
478
481
positions[vertex_i] = positions[old_vertex_i];
479
- uvs[vertex_i] = { 127 , uvs[old_vertex_i].y };
482
+ uvs[vertex_i] = { UnityUV , uvs[old_vertex_i].y };
480
483
normals[vertex_i] = normals[old_vertex_i];
481
484
482
485
vertex_i++;
@@ -583,12 +586,11 @@ core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createCylinder(
583
586
584
587
positions[i] = { p.x , p.y , p.z };
585
588
memcpy (normals + i, &n, sizeof (n));
586
- float32_t2 f32_uv = { f_i * tesselationRec, 0 .f };
587
- encodePixels<get_uv_format<uv_element_t >(), float >(uvs + i, f32_uv.data .data );
589
+ encodeUv (uvs + i, float32_t2 (f_i * tesselationRec, 0 .f ));
588
590
589
591
positions[i + halfIx] = { p.x , p.y , length };
590
592
normals[i + halfIx] = normals[i];
591
- uvs[i + halfIx] = { UnityUV, 0 };
593
+ uvs[i + halfIx] = { 1 . f * tesselationRec, UnityUV };
592
594
}
593
595
594
596
CPolygonGeometryManipulator::recomputeContentHashes (retval.get ());
@@ -602,9 +604,7 @@ core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createCone(
602
604
603
605
using namespace hlsl ;
604
606
605
- CQuantNormalCache* const quantNormalCache = quantNormalCacheOverride == nullptr ? m_params.normalCache .get () : quantNormalCacheOverride;
606
-
607
- const uint32_t u32_vertexCount = 2 * tesselation;
607
+ const uint32_t u32_vertexCount = tesselation + 1 ;
608
608
if (u32_vertexCount > std::numeric_limits<uint16_t >::max ())
609
609
return nullptr ;
610
610
const auto vertexCount = static_cast <uint16_t >(u32_vertexCount);
@@ -615,31 +615,25 @@ core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createCone(
615
615
// Create indices
616
616
using index_t = uint16_t ;
617
617
{
618
- constexpr uint32_t RowCount = 2u ;
619
618
const auto IndexCount = 3 * tesselation;
620
619
621
620
auto indexView = createIndexView<index_t >(IndexCount, vertexCount - 1 );
622
621
auto u = reinterpret_cast <index_t *>(indexView.src .buffer ->getPointer ());
623
622
624
- const uint32_t firstIndexOfBaseVertices = 0 ;
625
- const uint32_t firstIndexOfApexVertices = tesselation;
623
+ const uint32_t apexVertexIndex = tesselation;
626
624
627
625
for (uint32_t i = 0 ; i < tesselation; i++)
628
626
{
629
- u[i * 3 ] = firstIndexOfApexVertices + i ;
630
- u[(i * 3 ) + 1 ] = firstIndexOfBaseVertices + i;
631
- u[(i * 3 ) + 2 ] = i == (tesselation - 1 ) ? firstIndexOfBaseVertices : firstIndexOfBaseVertices + i + 1 ;
627
+ u[i * 3 ] = apexVertexIndex ;
628
+ u[(i * 3 ) + 1 ] = i;
629
+ u[(i * 3 ) + 2 ] = i == (tesselation - 1 ) ? 0 : i + 1 ;
632
630
}
633
631
634
632
retval->setIndexView (std::move (indexView));
635
633
}
636
634
637
- constexpr auto NormalCacheFormat = EF_R8G8B8_SNORM;
638
- constexpr auto NormalFormat = EF_R8G8B8A8_SNORM;
639
-
640
635
// Create vertex attributes with NONE usage because we have no clue how they'll be used
641
636
hlsl::float32_t3* positions;
642
- snorm_normal_t * normals;
643
637
{
644
638
{
645
639
shapes::AABB<4 , float32_t > aabb;
@@ -649,14 +643,6 @@ core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createCone(
649
643
positions = reinterpret_cast <decltype (positions)>(positionView.src .buffer ->getPointer ());
650
644
retval->setPositionView (std::move (positionView));
651
645
}
652
- {
653
- shapes::AABB<4 , int8_t > aabb;
654
- aabb.maxVx = snorm_all_ones;
655
- aabb.minVx = -aabb.maxVx ;
656
- auto normalView = createSnormNormalView (vertexCount, aabb);
657
- normals = reinterpret_cast <decltype (normals)>(normalView.src .buffer ->getPointer ());
658
- retval->setNormalView (std::move (normalView));
659
- }
660
646
}
661
647
662
648
const float step = (2 .f *core::PI<float >()) / tesselation;
@@ -669,38 +655,15 @@ core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createCone(
669
655
{
670
656
hlsl::float32_t3 v (std::cos (i * step), 0 .0f , std::sin (i * step));
671
657
v *= radius;
672
-
673
- positions[i] = { v.x , v.y , v.z };
674
- positions[apexVertexBase_i + i] = { apexVertexCoords.x , apexVertexCoords.y , apexVertexCoords.z };
675
-
676
- const auto simdPosition = hlsl::float32_t3 (positions[i].x , positions[i].y , positions[i].z );
677
- const hlsl::float32_t3 v0ToApex = apexVertexCoords - simdPosition;
678
-
679
- uint32_t nextVertexIndex = i == (tesselation - 1 ) ? 0 : i + 1 ;
680
- hlsl::float32_t3 u1 = hlsl::float32_t3 (positions[nextVertexIndex].x , positions[nextVertexIndex].y , positions[nextVertexIndex].z );
681
- u1 -= simdPosition;
682
- float angleWeight = std::acos (hlsl::dot (hlsl::normalize (apexVertexCoords), hlsl::normalize (u1)));
683
- u1 = hlsl::normalize (hlsl::cross (v0ToApex, u1)) * angleWeight;
684
-
685
- uint32_t prevVertexIndex = i == 0 ? (tesselation - 1 ) : i - 1 ;
686
- hlsl::float32_t3 u2 = hlsl::float32_t3 (positions[prevVertexIndex].x , positions[prevVertexIndex].y , positions[prevVertexIndex].z );
687
- u2 -= simdPosition;
688
- angleWeight = std::acos (hlsl::dot (hlsl::normalize (apexVertexCoords), hlsl::normalize (u2)));
689
- u2 = hlsl::normalize (hlsl::cross (u2, v0ToApex)) * angleWeight;
690
-
691
-
692
- const auto baseNormal = quantNormalCache->quantize <NormalCacheFormat>(hlsl::normalize (u1 + u2));
693
- memcpy (normals + i, &baseNormal, sizeof (baseNormal));
694
-
695
- const auto apexNormal = quantNormalCache->quantize <NormalCacheFormat>(hlsl::normalize (u1));
696
- memcpy (normals + apexVertexBase_i + i, &apexNormal, sizeof (apexNormal));
658
+ positions[i] = v;
697
659
}
660
+ positions[apexVertexBase_i] = apexVertexCoords;
698
661
699
662
CPolygonGeometryManipulator::recomputeContentHashes (retval.get ());
700
663
return retval;
701
664
}
702
665
703
- core::vector<core:: smart_refctd_ptr<ICPUPolygonGeometry> > CGeometryCreator::createArrow (
666
+ core::smart_refctd_ptr<ICPUGeometryCollection > CGeometryCreator::createArrow (
704
667
const uint16_t tesselationCylinder,
705
668
const uint16_t tesselationCone,
706
669
const float height,
@@ -711,25 +674,20 @@ core::vector<core::smart_refctd_ptr<ICPUPolygonGeometry>> CGeometryCreator::crea
711
674
{
712
675
assert (height > cylinderHeight);
713
676
714
- using position_t = hlsl::float32_t3;
715
-
716
677
auto cylinder = createCylinder (width0, cylinderHeight, tesselationCylinder);
717
678
auto cone = createCone (width1, height-cylinderHeight, tesselationCone);
718
679
719
- auto conePositions = reinterpret_cast <position_t *>(cone->getPositionView ().src .buffer ->getPointer ());
720
-
721
- const auto coneVertexCount = cone->getPositionView ().getElementCount ();
722
-
723
- for (auto i = 0ull ; i < coneVertexCount; ++i)
724
- {
725
- auto & conePosition = conePositions[i];
726
- core::vector3df_SIMD newPos (conePosition.x , conePosition.y , conePosition.z );
727
- newPos.rotateYZByRAD (-1.5707963268 );
728
-
729
- conePosition = {newPos.x , newPos.y , newPos.z };
730
- }
731
-
732
- return {cylinder, cone};
680
+ auto collection = core::make_smart_refctd_ptr<ICPUGeometryCollection>();
681
+ auto * geometries = collection->getGeometries ();
682
+ geometries->push_back ({
683
+ .geometry = cylinder
684
+ });
685
+ const auto coneTransform = hlsl::math::linalg::rotation_mat (-1 .5707963268f , hlsl::float32_t3 (1 .f , 0 .f , 0 .f ));
686
+ geometries->push_back ({
687
+ .transform = hlsl::float32_t3x4 (coneTransform),
688
+ .geometry = cone
689
+ });
690
+ return collection;
733
691
734
692
}
735
693
@@ -1855,9 +1813,7 @@ core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createIcoSphere(fl
1855
1813
for (auto uv_i = 0u ; uv_i < icosphere.getVertexCount (); uv_i++)
1856
1814
{
1857
1815
const auto texCoords = icosphere.getTexCoords ();
1858
- const auto f32_uv = float32_t2{ texCoords[2 * uv_i], texCoords[(2 * uv_i) + 1 ] };
1859
- const auto u32_uv = packUnorm2x16 (f32_uv);
1860
- memcpy (uvs + uv_i, &u32_uv, sizeof (u32_uv));
1816
+ encodeUv (uvs + uv_i, float32_t2 (texCoords[2 * uv_i], texCoords[(2 * uv_i) + 1 ]));
1861
1817
}
1862
1818
1863
1819
retval->getAuxAttributeViews ()->push_back (std::move (uvView));
0 commit comments