|
4 | 4 |
|
5 | 5 |
|
6 | 6 | #include "nbl/asset/utils/CGeometryCreator.h"
|
| 7 | +#include "nbl/builtin/hlsl/tgmath.hlsl" |
7 | 8 |
|
8 | 9 | #include <iostream>
|
9 | 10 | #include <iomanip>
|
@@ -99,7 +100,7 @@ core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createCube(const h
|
99 | 100 | uvs = reinterpret_cast<decltype(uvs)>(buff->getPointer());
|
100 | 101 | shapes::AABB<4,uint8_t> aabb;
|
101 | 102 | 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); |
103 | 104 | retval->getAuxAttributeViews()->push_back({
|
104 | 105 | .composed = {
|
105 | 106 | .encodedDataRange = {.u8=aabb},
|
@@ -164,10 +165,10 @@ core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createCube(const h
|
164 | 165 | };
|
165 | 166 | const hlsl::vector<uint8_t, 2> uv[4] =
|
166 | 167 | {
|
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) |
171 | 172 | };
|
172 | 173 | for (size_t f=0ull; f<6ull; ++f)
|
173 | 174 | {
|
@@ -647,113 +648,209 @@ core::smart_refctd_ptr<ICPUPolygonGeometry> CGeometryCreator::createCone(
|
647 | 648 |
|
648 | 649 | return cone;
|
649 | 650 | }
|
| 651 | +#endif |
650 | 652 |
|
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 |
653 | 654 | {
|
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; |
666 | 656 |
|
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 | + } |
683 | 686 |
|
684 | 687 | // 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 | + } |
699 | 753 |
|
700 | 754 | return retval;
|
701 | 755 | }
|
702 | 756 |
|
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 |
704 | 758 | {
|
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; |
719 | 762 |
|
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; |
731 | 764 |
|
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()); |
735 | 767 |
|
736 |
| - //vn |
737 |
| - ptr[vertexCount - 1] = ptr[1]; |
| 768 | + // without index buffer |
| 769 | + const size_t vertexCount = 2u + tesselation; |
738 | 770 |
|
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; |
741 | 777 | {
|
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 | + } |
749 | 829 | }
|
750 | 830 |
|
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)); |
753 | 849 |
|
754 | 850 | return retval;
|
755 | 851 | }
|
756 | 852 |
|
| 853 | +#if 0 |
757 | 854 | /*
|
758 | 855 | Helpful Icosphere class implementation used to compute
|
759 | 856 | and create icopshere's vertices and indecies.
|
|
0 commit comments