10
10
// ! I advise to check out this file, its a basic input handler
11
11
#include " ../common/QToQuitEventReceiver.h"
12
12
13
+ #include " nbl/ext/DebugDraw/CDraw3DLine.h"
14
+
13
15
using namespace nbl ;
14
16
using namespace nbl ::core;
15
17
using namespace nbl ::asset;
16
18
using namespace nbl ::video;
17
19
18
20
#include " common.h"
21
+ #include " rasterizationCommon.h"
19
22
20
23
// vt stuff
21
24
using STextureData = asset::ICPUVirtualTexture::SMasterTextureData;
@@ -31,6 +34,8 @@ struct commit_t
31
34
asset::ICPUSampler::E_TEXTURE_CLAMP uwrap;
32
35
asset::ICPUSampler::E_TEXTURE_CLAMP vwrap;
33
36
asset::ICPUSampler::E_TEXTURE_BORDER_COLOR border;
37
+
38
+ core::vector<CullData_t> cullData;
34
39
};
35
40
36
41
constexpr uint32_t TEX_OF_INTEREST_CNT = 6u ;
@@ -155,6 +160,18 @@ struct SceneData
155
160
smart_refctd_ptr<IGPUBuffer> ubo;
156
161
};
157
162
163
+ struct CullShaderData
164
+ {
165
+ core::smart_refctd_ptr<IGPUBuffer> perBatchCull;
166
+ core::smart_refctd_ptr<IGPUBuffer> commandBuffer;
167
+
168
+ core::smart_refctd_ptr<IGPUComputePipeline> cullPipeline;
169
+ core::smart_refctd_ptr<IGPUDescriptorSetLayout> cullDSLayout;
170
+ core::smart_refctd_ptr<IGPUDescriptorSet> cullDS;
171
+
172
+ uint32_t maxBatchCount;
173
+ };
174
+
158
175
using MeshPacker = CCPUMeshPackerV2<DrawElementsIndirectCommand_t>;
159
176
using GPUMeshPacker = CGPUMeshPackerV2<DrawElementsIndirectCommand_t>;
160
177
@@ -212,6 +229,8 @@ int main()
212
229
auto * am = device->getAssetManager ();
213
230
auto * fs = am->getFileSystem ();
214
231
232
+ auto draw3DLine = ext::DebugDraw::CDraw3DLine::create (driver);
233
+
215
234
//
216
235
auto createScreenSizedImage = [driver,¶ms](const E_FORMAT format) -> auto
217
236
{
@@ -250,6 +269,8 @@ int main()
250
269
251
270
//
252
271
SceneData sceneData;
272
+ CullShaderData cullShaderData;
273
+ core::vector<std::pair<ext::DebugDraw::S3DLineVertex, ext::DebugDraw::S3DLineVertex>> dbgLines;
253
274
{
254
275
//
255
276
smart_refctd_ptr<IGPUDescriptorSetLayout> perFrameDSLayout,shadingDSLayout;
@@ -507,6 +528,9 @@ int main()
507
528
core::vector<BatchInstanceData> batchData;
508
529
batchData.reserve (mdiCntBound);
509
530
531
+ core::vector<CullData_t> batchCullData (mdiCntBound);
532
+ auto batchCullDataEnd = batchCullData.begin ();
533
+
510
534
allocDataIt = allocData->begin ();
511
535
uint32_t mdiListOffset = 0u ;
512
536
for (auto it=pipelineMeshBufferRanges.begin (); it!=pipelineMeshBufferRanges.end ()-1u ; )
@@ -517,7 +541,8 @@ int main()
517
541
const uint32_t meshMdiBound = mp->calcMDIStructMaxCount (mbRangeBegin,mbRangeEnd);
518
542
core::vector<IMeshPackerBase::PackedMeshBufferData> pmbd (std::distance (mbRangeBegin,mbRangeEnd));
519
543
core::vector<MeshPacker::CombinedDataOffsetTable> cdot (meshMdiBound);
520
- uint32_t actualMdiCnt = mp->commit (pmbd.data (),cdot.data (),nullptr ,&*allocDataIt,mbRangeBegin,mbRangeEnd);
544
+ core::vector<aabbox3df> aabbs (meshMdiBound);
545
+ uint32_t actualMdiCnt = mp->commit (pmbd.data (),cdot.data (),aabbs.data (),&*allocDataIt,mbRangeBegin,mbRangeEnd);
521
546
allocDataIt += meshMdiBound;
522
547
523
548
if (actualMdiCnt==0u )
@@ -526,6 +551,24 @@ int main()
526
551
_NBL_DEBUG_BREAK_IF (true );
527
552
}
528
553
554
+ for (uint32_t i = 0u ; i < actualMdiCnt; i++)
555
+ {
556
+ batchCullDataEnd->aabbMinEdge .x = aabbs[i].MinEdge .X ;
557
+ batchCullDataEnd->aabbMinEdge .y = aabbs[i].MinEdge .Y ;
558
+ batchCullDataEnd->aabbMinEdge .z = aabbs[i].MinEdge .Z ;
559
+
560
+ batchCullDataEnd->aabbMaxEdge .x = aabbs[i].MaxEdge .X ;
561
+ batchCullDataEnd->aabbMaxEdge .y = aabbs[i].MaxEdge .Y ;
562
+ batchCullDataEnd->aabbMaxEdge .z = aabbs[i].MaxEdge .Z ;
563
+
564
+ batchCullDataEnd->drawCommandGUID = pmbd[i].mdiParameterOffset + i;
565
+ assert (pmbd[i].mdiParameterOffset + i <= pmbd[i].mdiParameterCount );
566
+
567
+ draw3DLine->enqueueBox (dbgLines, aabbs[i], 1 .0f , 0 .0f , 0 .0f , 1 .0f , core::matrix3x4SIMD ());
568
+
569
+ batchCullDataEnd++;
570
+ }
571
+
529
572
sceneData.pushConstantsData .push_back (mdiListOffset);
530
573
mdiListOffset += actualMdiCnt;
531
574
@@ -558,6 +601,10 @@ int main()
558
601
gpump = core::make_smart_refctd_ptr<CGPUMeshPackerV2<>>(driver,mp.get ());
559
602
sceneData.mdiBuffer = gpump->getPackerDataStore ().MDIDataBuffer ;
560
603
sceneData.idxBuffer = gpump->getPackerDataStore ().indexBuffer ;
604
+
605
+ cullShaderData.commandBuffer = gpump->getPackerDataStore ().MDIDataBuffer ;
606
+ cullShaderData.maxBatchCount = std::distance (batchCullData.begin (), batchCullDataEnd);
607
+ cullShaderData.perBatchCull = driver->createFilledDeviceLocalGPUBufferOnDedMem (cullShaderData.maxBatchCount , batchCullData.data ());
561
608
}
562
609
mesh_raw->convertToDummyObject (~0u );
563
610
@@ -738,6 +785,83 @@ int main()
738
785
}
739
786
}
740
787
788
+ {
789
+ SPushConstantRange range{ ISpecializedShader::ESS_COMPUTE,0u ,sizeof (CullShaderData_t) };
790
+
791
+ {
792
+ IGPUDescriptorSetLayout::SBinding bindings[2 ];
793
+ bindings[0 ].binding = 0u ;
794
+ bindings[0 ].count = 1u ;
795
+ bindings[0 ].samplers = nullptr ;
796
+ bindings[0 ].stageFlags = ISpecializedShader::ESS_COMPUTE;
797
+ bindings[0 ].type = EDT_STORAGE_BUFFER;
798
+
799
+ bindings[1 ].binding = 1u ;
800
+ bindings[1 ].count = 1u ;
801
+ bindings[1 ].samplers = nullptr ;
802
+ bindings[1 ].stageFlags = ISpecializedShader::ESS_COMPUTE;
803
+ bindings[1 ].type = EDT_STORAGE_BUFFER;
804
+
805
+ cullShaderData.cullDSLayout = driver->createGPUDescriptorSetLayout (bindings, bindings + sizeof (bindings) / sizeof (IGPUDescriptorSetLayout::SBinding));
806
+ }
807
+
808
+ {
809
+ IGPUDescriptorSet::SDescriptorInfo infos[2 ];
810
+
811
+ infos[0 ].desc = core::smart_refctd_ptr (cullShaderData.perBatchCull );
812
+ infos[0 ].buffer .offset = 0u ;
813
+ infos[0 ].buffer .size = cullShaderData.perBatchCull ->getSize ();
814
+
815
+ infos[1 ].desc = core::smart_refctd_ptr (cullShaderData.commandBuffer );
816
+ infos[1 ].buffer .offset = 0u ;
817
+ infos[1 ].buffer .size = cullShaderData.commandBuffer ->getSize ();
818
+
819
+ cullShaderData.cullDS = driver->createGPUDescriptorSet (smart_refctd_ptr (cullShaderData.cullDSLayout ));
820
+
821
+ IGPUDescriptorSet::SWriteDescriptorSet writes[2 ];
822
+
823
+ for (uint32_t i = 0u ; i < 2 ; i++)
824
+ {
825
+ writes[i].dstSet = cullShaderData.cullDS .get ();
826
+ writes[i].binding = i;
827
+ writes[i].arrayElement = 0u ;
828
+ writes[i].count = 1u ;
829
+ writes[i].descriptorType = EDT_STORAGE_BUFFER;
830
+ writes[i].info = infos + i;
831
+ }
832
+
833
+ driver->updateDescriptorSets (sizeof (writes) / sizeof (IGPUDescriptorSet::SWriteDescriptorSet), writes, 0u , nullptr );
834
+ }
835
+
836
+ asset::IAssetLoader::SAssetLoadParams lp;
837
+ auto cullShader = IAsset::castDown<ICPUSpecializedShader>(*am->getAsset (" ../cull.comp" , lp).getContents ().begin ());
838
+ assert (cullShader);
839
+ const asset::ICPUShader* unspec = cullShader->getUnspecialized ();
840
+ assert (unspec->containsGLSL ());
841
+
842
+ auto gpuCullShader = driver->getGPUObjectsFromAssets (&cullShader, &cullShader + 1u )->begin ()[0 ];
843
+
844
+ auto cullPipelineLayout = driver->createGPUPipelineLayout (&range, &range + 1u , core::smart_refctd_ptr (cullShaderData.cullDSLayout ));
845
+ cullShaderData.cullPipeline = driver->createGPUComputePipeline (nullptr , std::move (cullPipelineLayout), std::move (gpuCullShader));
846
+ }
847
+
848
+ auto cullBatches = [&driver, &cullShaderData](const core::matrix4SIMD& vp)
849
+ {
850
+ driver->bindDescriptorSets (EPBP_COMPUTE, cullShaderData.cullPipeline ->getLayout (), 0u , 1u , &cullShaderData.cullDS .get (), nullptr );
851
+ driver->bindComputePipeline (cullShaderData.cullPipeline .get ());
852
+
853
+ CullShaderData_t cullPushConstants;
854
+ cullPushConstants.viewProjMatrix = vp;
855
+ cullPushConstants.viewProjDeterminant = core::determinant (vp);
856
+ cullPushConstants.maxBatchCount = cullShaderData.maxBatchCount ; // TODO: this should be uniform, and set only once
857
+
858
+ driver->pushConstants (cullShaderData.cullPipeline ->getLayout (), ISpecializedShader::ESS_COMPUTE, 0u , sizeof (CullShaderData_t), &cullPushConstants);
859
+
860
+ const uint32_t cullWorkGroups = (cullPushConstants.maxBatchCount - 1u ) / WORKGROUP_SIZE + 1u ;
861
+
862
+ driver->dispatch (cullWorkGroups, 1u , 1u );
863
+ };
864
+
741
865
// ! we want to move around the scene and view it from different angles
742
866
scene::ICameraSceneNode* camera = smgr->addCameraSceneNodeFPS (0 , 100 .0f , 0 .5f );
743
867
@@ -748,6 +872,8 @@ int main()
748
872
749
873
smgr->setActiveCamera (camera);
750
874
875
+ // tmp shit
876
+ core::matrix4SIMD vpFromFirstFrame;
751
877
752
878
uint64_t lastFPSTime = 0 ;
753
879
while (device->run () && receiver.keepOpen ())
@@ -758,13 +884,17 @@ int main()
758
884
camera->OnAnimate (std::chrono::duration_cast<std::chrono::milliseconds>(device->getTimer ()->getTime ()).count ());
759
885
camera->render ();
760
886
887
+ if (lastFPSTime == 0 )
888
+ vpFromFirstFrame = camera->getConcatenatedMatrix ();
889
+
761
890
SBasicViewParameters uboData;
762
891
memcpy (uboData.MVP , camera->getConcatenatedMatrix ().pointer (), sizeof (core::matrix4SIMD));
763
892
memcpy (uboData.MV , camera->getViewMatrix ().pointer (), sizeof (core::matrix3x4SIMD));
764
893
memcpy (uboData.NormalMat , camera->getViewMatrix ().pointer (), sizeof (core::matrix3x4SIMD));
765
894
driver->updateBufferRangeViaStagingBuffer (sceneData.ubo .get (), 0u , sizeof (SBasicViewParameters), &uboData);
766
895
767
896
// TODO: Cull MDIs
897
+ cullBatches (vpFromFirstFrame);
768
898
769
899
driver->setRenderTarget (visBuffer);
770
900
driver->clearZBuffer ();
@@ -788,6 +918,9 @@ int main()
788
918
);
789
919
}
790
920
921
+ // draw aabbs
922
+ draw3DLine->draw (camera->getConcatenatedMatrix (), dbgLines);
923
+
791
924
// shade
792
925
driver->bindDescriptorSets (video::EPBP_COMPUTE,sceneData.shadeVBufferPpln ->getLayout (),0u ,4u ,ds,nullptr );
793
926
driver->bindComputePipeline (sceneData.shadeVBufferPpln .get ());
@@ -800,6 +933,7 @@ int main()
800
933
801
934
// blit
802
935
driver->blitRenderTargets (fb,0 );
936
+
803
937
driver->endScene ();
804
938
805
939
// display frames per second in window title
0 commit comments