3
3
// For conditions of distribution and use, see copyright notice in nabla.h
4
4
5
5
#include < nabla.h>
6
+ #include " nbl/asset/utils/CGeometryCreator.h"
6
7
#include " nbl/video/utilities/CSimpleResizeSurface.h"
7
8
8
9
#include " ../common/SimpleWindowedApplication.hpp"
12
13
#include " this_example/spirv/builtin/CArchive.h"
13
14
#include " this_example/spirv/builtin/builtinResources.h"
14
15
15
- #include " shaders/cube.common.hlsl"
16
- #include " shaders/grid.common.hlsl"
16
+ #include " shaders/common.hlsl"
17
17
18
18
using namespace nbl ;
19
19
using namespace core ;
@@ -268,6 +268,9 @@ class GizmoApp final : public examples::SimpleWindowedApplication
268
268
m_winMgr->setWindowSize (m_window.get (), WIN_W, WIN_H);
269
269
m_surface->recreateSwapchain ();
270
270
271
+ auto assetManager = make_smart_refctd_ptr<nbl::asset::IAssetManager>(smart_refctd_ptr (system));
272
+ auto * geometry = assetManager->getGeometryCreator ();
273
+
271
274
SPushConstantRange pushConstantRanges[] = {
272
275
{
273
276
.stageFlags = IShader::ESS_VERTEX,
@@ -305,98 +308,23 @@ class GizmoApp final : public examples::SimpleWindowedApplication
305
308
if (!pipelineLayout)
306
309
return logFail (" Could not create Pipeline Layout!" );
307
310
308
- SPrimitiveAssemblyParams primitiveAssemblyParams{};
309
- {
310
- primitiveAssemblyParams.primitiveType = EPT_TRIANGLE_LIST;
311
- }
312
-
313
- SVertexInputParams vertexInputParams{};
314
- {
315
- vertexInputParams.enabledBindingFlags = 0b1u ;
316
- vertexInputParams.enabledAttribFlags = 0b11u ;
317
-
318
- vertexInputParams.bindings [0 ].inputRate = asset::SVertexInputBindingParams::EVIR_PER_VERTEX;
319
- vertexInputParams.bindings [0 ].stride = sizeof (cube::VSInput);
311
+ const auto cube = geometry->createCubeMesh (vector3df (1 .f , 1 .f , 1 .f ));
312
+ const auto grid = geometry->createRectangleMesh (vector2df_SIMD (999 .f , 999 .f ));
320
313
321
- auto & position = vertexInputParams.attributes [0 ];
322
-
323
- position.format = EF_R32G32B32A32_SFLOAT;
324
- position.relativeOffset = offsetof (cube::VSInput, position);
325
- position.binding = 0u ;
326
-
327
- auto & color = vertexInputParams.attributes [1 ];
328
- color.format = EF_R8G8B8A8_UNORM;
329
- color.relativeOffset = offsetof (cube::VSInput, color);
330
- color.binding = 0u ;
331
- }
332
-
333
- if (!createPipeline<NBL_CORE_UNIQUE_STRING_LITERAL_TYPE (" gizmo/spirv/cube.vertex.spv" ), NBL_CORE_UNIQUE_STRING_LITERAL_TYPE (" gizmo/spirv/cube.fragment.spv" )>(EP_CUBE, vertexInputParams, primitiveAssemblyParams, pipelineLayout.get (), renderpass))
314
+ if (!createPipeline<NBL_CORE_UNIQUE_STRING_LITERAL_TYPE (" gizmo/spirv/cube.vertex.spv" ), NBL_CORE_UNIQUE_STRING_LITERAL_TYPE (" gizmo/spirv/cube.fragment.spv" )>(EP_CUBE, cube, pipelineLayout.get (), renderpass))
334
315
return logFail (" Could not create pipeline for cube pass!" );
335
316
336
- {
337
- vertexInputParams.bindings [0 ].stride = sizeof (grid::VSInput);
338
-
339
- auto & position = vertexInputParams.attributes [0 ];
340
- position.format = EF_R32G32B32_SFLOAT;
341
- position.relativeOffset = offsetof (grid::VSInput, position);
342
-
343
- auto & color = vertexInputParams.attributes [1 ];
344
- color.format = EF_R32G32_SFLOAT;
345
- color.relativeOffset = offsetof (grid::VSInput, uv);
346
-
347
- if (!createPipeline<NBL_CORE_UNIQUE_STRING_LITERAL_TYPE (" gizmo/spirv/grid.vertex.spv" ), NBL_CORE_UNIQUE_STRING_LITERAL_TYPE (" gizmo/spirv/grid.fragment.spv" ) > (EP_GRID, vertexInputParams, primitiveAssemblyParams, pipelineLayout.get (), renderpass))
348
- return logFail (" Could not create pipeline for grid pass!" );
349
- }
350
-
351
- auto getRandomColor = []()
352
- {
353
- static std::random_device rd;
354
- static std::mt19937 gen (rd ());
355
- static std::uniform_real_distribution<float > dis (0 .0f , 1 .0f );
356
-
357
- return dis (gen);
358
- };
359
-
360
- // cube
361
- const auto cubeVertices = std::to_array (
362
- {
363
- cube::VSInput{{-1 .0f , -1 .0f , -1 .0f , 1 .0f }, {getRandomColor (), getRandomColor (), getRandomColor (), 1 .0f }}, // vertex 0
364
- cube::VSInput{{ 1 .0f , -1 .0f , -1 .0f , 1 .0f }, {getRandomColor (), getRandomColor (), getRandomColor (), 1 .0f }}, // vertex 1
365
- cube::VSInput{{ 1 .0f , 1 .0f , -1 .0f , 1 .0f }, {getRandomColor (), getRandomColor (), getRandomColor (), 1 .0f }}, // vertex 2
366
- cube::VSInput{{-1 .0f , 1 .0f , -1 .0f , 1 .0f }, {getRandomColor (), getRandomColor (), getRandomColor (), 1 .0f }}, // vertex 3
367
- cube::VSInput{{-1 .0f , -1 .0f , 1 .0f , 1 .0f }, {getRandomColor (), getRandomColor (), getRandomColor (), 1 .0f }}, // vertex 4
368
- cube::VSInput{{ 1 .0f , -1 .0f , 1 .0f , 1 .0f }, {getRandomColor (), getRandomColor (), getRandomColor (), 1 .0f }}, // vertex 5
369
- cube::VSInput{{ 1 .0f , 1 .0f , 1 .0f , 1 .0f }, {getRandomColor (), getRandomColor (), getRandomColor (), 1 .0f }}, // vertex 6
370
- cube::VSInput{{-1 .0f , 1 .0f , 1 .0f , 1 .0f }, {getRandomColor (), getRandomColor (), getRandomColor (), 1 .0f }} // vertex 7
371
- });
372
-
373
- const auto cubeIndices = std::to_array<uint16_t >(
374
- {
375
- // Front face (0, 1, 2, 3)
376
- 0 , 1 , 2 , 2 , 3 , 0 ,
377
- // Back face (4, 5, 6, 7)
378
- 4 , 5 , 6 , 6 , 7 , 4 ,
379
- // Left face (0, 3, 7, 4)
380
- 0 , 3 , 7 , 7 , 4 , 0 ,
381
- // Right face (1, 2, 6, 5)
382
- 1 , 5 , 6 , 6 , 2 , 1 ,
383
- // Top face (3, 2, 6, 7)
384
- 3 , 2 , 6 , 6 , 7 , 3 ,
385
- // Bottom face (0, 1, 5, 4)
386
- 0 , 1 , 5 , 5 , 4 , 0
387
- });
388
-
389
- const auto gridVertices = generateGridVertices ();
390
- const auto gridIndices = generateGridIndices ();
317
+ if (!createPipeline<NBL_CORE_UNIQUE_STRING_LITERAL_TYPE (" gizmo/spirv/grid.vertex.spv" ), NBL_CORE_UNIQUE_STRING_LITERAL_TYPE (" gizmo/spirv/grid.fragment.spv" )>(EP_GRID, grid, pipelineLayout.get (), renderpass))
318
+ return logFail (" Could not create pipeline for grid pass!" );
391
319
392
320
// buffers
393
321
{
394
322
const auto mask = m_device->getPhysicalDevice ()->getUpStreamingMemoryTypeBits ();
395
323
396
- if (!createVIBuffers (EP_CUBE, cubeVertices, cubeIndices , mask))
324
+ if (!createVIBuffers (EP_CUBE, cube , mask))
397
325
return false ;
398
326
399
- if (!createVIBuffers (EP_GRID, gridVertices, gridIndices , mask))
327
+ if (!createVIBuffers (EP_GRID, grid , mask))
400
328
return false ;
401
329
402
330
m_ubo = m_device->createBuffer ({{.size = sizeof (SBasicViewParameters), .usage = core::bitflag (asset::IBuffer::EUF_UNIFORM_BUFFER_BIT) | asset::IBuffer::EUF_TRANSFER_DST_BIT | asset::IBuffer::EUF_INLINE_UPDATE_VIA_CMDBUF} });
@@ -556,10 +484,10 @@ class GizmoApp final : public examples::SimpleWindowedApplication
556
484
cb->pushConstants (rawPipeline->getLayout (), IShader::ESS_VERTEX, 0 , sizeof (PushConstants), &m_pc);
557
485
558
486
const asset::SBufferBinding<const IGPUBuffer> bVertices[] = { {.offset = 0 , .buffer = hook.m_vertexBuffer } };
559
- const asset::SBufferBinding<const IGPUBuffer> bIndex = { .offset = 0 , .buffer = hook.m_indexBuffer };
487
+ const asset::SBufferBinding<const IGPUBuffer> bIndices = { .offset = 0 , .buffer = hook.m_indexBuffer };
560
488
561
489
cb->bindVertexBuffers (0 , 1 , bVertices);
562
- cb->bindIndexBuffer (bIndex , EIT_16BIT);
490
+ cb->bindIndexBuffer (bIndices , EIT_16BIT); // geometry creator uses 16 bit for indicies
563
491
cb->drawIndexed (hook.m_indexBuffer ->getSize () / sizeof (uint16_t ), 1 , 0 , 0 , 0 );
564
492
};
565
493
@@ -663,7 +591,7 @@ class GizmoApp final : public examples::SimpleWindowedApplication
663
591
PushConstants m_pc = {.withGizmo = true };
664
592
665
593
template <nbl::core::StringLiteral vPath, nbl::core::StringLiteral fPath >
666
- bool createPipeline (E_PASS ep, const SVertexInputParams& vip, const SPrimitiveAssemblyParams& pas , const video::IGPUPipelineLayout* pl, const video::IGPURenderpass* rp)
594
+ bool createPipeline (E_PASS ep, const CGeometryCreator::return_type& oData , const video::IGPUPipelineLayout* pl, const video::IGPURenderpass* rp)
667
595
{
668
596
struct
669
597
{
@@ -678,12 +606,12 @@ class GizmoApp final : public examples::SimpleWindowedApplication
678
606
} spirv;
679
607
680
608
auto createShader = [&](const system::SBuiltinFile& in, asset::IShader::E_SHADER_STAGE stage) -> core::smart_refctd_ptr<video::IGPUShader>
681
- {
682
- const auto buffer = core::make_smart_refctd_ptr<asset::CCustomAllocatorCPUBuffer<core::null_allocator<uint8_t >, true > >(in.size , (void *)in.contents , core::adopt_memory);
683
- const auto shader = make_smart_refctd_ptr<ICPUShader>(core::smart_refctd_ptr (buffer), stage, IShader::E_CONTENT_TYPE::ECT_SPIRV, " " );
609
+ {
610
+ const auto buffer = core::make_smart_refctd_ptr<asset::CCustomAllocatorCPUBuffer<core::null_allocator<uint8_t >, true > >(in.size , (void *)in.contents , core::adopt_memory);
611
+ const auto shader = make_smart_refctd_ptr<ICPUShader>(core::smart_refctd_ptr (buffer), stage, IShader::E_CONTENT_TYPE::ECT_SPIRV, " " );
684
612
685
- return m_device->createShader (shader.get ());
686
- };
613
+ return m_device->createShader (shader.get ());
614
+ };
687
615
688
616
shaders.vertex = createShader (spirv.vertex , IShader::ESS_VERTEX);
689
617
shaders.fragment = createShader (spirv.fragment , IShader::ESS_FRAGMENT);
@@ -719,19 +647,21 @@ class GizmoApp final : public examples::SimpleWindowedApplication
719
647
param.layout = pl;
720
648
param.shaders = specs;
721
649
param.renderpass = rp;
722
- param.cached = { .vertexInput = vip , .primitiveAssembly = pas , .rasterization = rasterizationParams, .blend = blendParams, .subpassIx = 0u };
650
+ param.cached = { .vertexInput = oData. inputParams , .primitiveAssembly = oData. assemblyParams , .rasterization = rasterizationParams, .blend = blendParams, .subpassIx = 0u };
723
651
};
724
652
725
653
return m_device->createGraphicsPipelines (nullptr , params, &pass[ep].pipeline );
726
654
}
727
655
}
728
656
729
- bool createVIBuffers (E_PASS ep, const auto & vBuffer, const auto & iBuffer , auto mask)
657
+ bool createVIBuffers (E_PASS ep, const CGeometryCreator::return_type& oData , auto mask)
730
658
{
731
659
auto & hook = pass[ep];
660
+ auto vBuffer = core::smart_refctd_ptr (oData.bindings [0 ].buffer ); // no offset
661
+ auto iBuffer = core::smart_refctd_ptr (oData.indexBuffer .buffer ); // no offset
732
662
733
- hook.m_vertexBuffer = m_device->createBuffer ({ {.size = sizeof ( vBuffer. front ()) * vBuffer. size () , .usage = core::bitflag (asset::IBuffer::EUF_VERTEX_BUFFER_BIT) | asset::IBuffer::EUF_TRANSFER_DST_BIT | asset::IBuffer::EUF_INLINE_UPDATE_VIA_CMDBUF} });
734
- hook.m_indexBuffer = m_device->createBuffer ({ {.size = sizeof ( iBuffer. front ()) * iBuffer. size () , .usage = core::bitflag (asset::IBuffer::EUF_INDEX_BUFFER_BIT) | asset::IBuffer::EUF_VERTEX_BUFFER_BIT | asset::IBuffer::EUF_TRANSFER_DST_BIT | asset::IBuffer::EUF_INLINE_UPDATE_VIA_CMDBUF} });
663
+ hook.m_vertexBuffer = m_device->createBuffer ({ {.size = vBuffer-> getSize () , .usage = core::bitflag (asset::IBuffer::EUF_VERTEX_BUFFER_BIT) | asset::IBuffer::EUF_TRANSFER_DST_BIT | asset::IBuffer::EUF_INLINE_UPDATE_VIA_CMDBUF}});
664
+ hook.m_indexBuffer = m_device->createBuffer ({ {.size = iBuffer-> getSize () , .usage = core::bitflag (asset::IBuffer::EUF_INDEX_BUFFER_BIT) | asset::IBuffer::EUF_VERTEX_BUFFER_BIT | asset::IBuffer::EUF_TRANSFER_DST_BIT | asset::IBuffer::EUF_INLINE_UPDATE_VIA_CMDBUF}});
735
665
736
666
for (auto it : { hook.m_vertexBuffer , hook.m_indexBuffer })
737
667
{
@@ -773,70 +703,18 @@ class GizmoApp final : public examples::SimpleWindowedApplication
773
703
}
774
704
}
775
705
776
- auto * vPointer = static_cast <cube::VSInput*>( vBinding.memory ->getMappedPointer () );
777
- auto * iPointer = static_cast < uint16_t *>( iBinding.memory ->getMappedPointer () );
706
+ auto * vPointer = vBinding.memory ->getMappedPointer ();
707
+ auto * iPointer = iBinding.memory ->getMappedPointer ();
778
708
779
- memcpy (vPointer, vBuffer. data (), hook.m_vertexBuffer ->getSize ());
780
- memcpy (iPointer, iBuffer. data (), hook.m_indexBuffer ->getSize ());
709
+ memcpy (vPointer, vBuffer-> getPointer (), hook.m_vertexBuffer ->getSize ());
710
+ memcpy (iPointer, iBuffer-> getPointer (), hook.m_indexBuffer ->getSize ());
781
711
782
712
vBinding.memory ->unmap ();
783
713
iBinding.memory ->unmap ();
784
714
785
715
return true ;
786
716
}
787
717
}
788
-
789
- std::vector<grid::VSInput> generateGridVertices ()
790
- {
791
- std::vector<grid::VSInput> vertices;
792
-
793
- std::vector<float > corners =
794
- {
795
- -999 .0f , -999 .0f , -0 .015f ,
796
- -999 .0f , 999 .0f , -0 .015f ,
797
- 999 .0f , 999 .0f , -0 .015f ,
798
- 999 .0f , -999 .0f , -0 .015f ,
799
- };
800
-
801
- std::vector<float > uvs =
802
- {
803
- 1 .0f , 0 .0f ,
804
- 0 .0f , 0 .0f ,
805
- 0 .0f , 1 .0f ,
806
- 1 .0f , 1 .0f ,
807
- };
808
-
809
- for (size_t i = 0 ; i < corners.size (); i += 12 )
810
- {
811
- for (size_t j = 0 ; j < 4 ; ++j) {
812
- grid::VSInput vertex;
813
- vertex.position [0 ] = corners[i + j * 3 + 0 ];
814
- vertex.position [1 ] = corners[i + j * 3 + 1 ];
815
- vertex.position [2 ] = corners[i + j * 3 + 2 ];
816
- vertex.uv [0 ] = uvs[j * 2 + 0 ];
817
- vertex.uv [1 ] = uvs[j * 2 + 1 ];
818
- vertices.push_back (vertex);
819
- }
820
- }
821
-
822
- return vertices;
823
- }
824
-
825
- std::vector<uint16_t > generateGridIndices ()
826
- {
827
- std::vector<uint16_t > indices;
828
-
829
- for (uint16_t i = 0 ; i < 4 ; i += 4 ) {
830
- indices.push_back (i + 0 );
831
- indices.push_back (i + 1 );
832
- indices.push_back (i + 2 );
833
- indices.push_back (i + 2 );
834
- indices.push_back (i + 3 );
835
- indices.push_back (i + 0 );
836
- }
837
-
838
- return indices;
839
- }
840
718
};
841
719
842
720
NBL_MAIN_FUNC (GizmoApp)
0 commit comments