Skip to content

Commit b2b2c90

Browse files
committed
Implemented culling
1 parent cdb5a71 commit b2b2c90

File tree

3 files changed

+68
-29
lines changed

3 files changed

+68
-29
lines changed

examples_tests/41.VisibilityBuffer/cull.comp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ layout(set=0, binding=1, std430) restrict coherent buffer IndirectDraws
1313
{
1414
nbl_glsl_DrawElementsIndirectCommand_t draws[];
1515
} commandBuff;
16+
layout(set=0, binding=2, std430) restrict coherent buffer MVPs
17+
{
18+
mat4 mvps[];
19+
} mvpBuff;
1620

1721

1822

@@ -32,6 +36,11 @@ void main()
3236
if (gl_GlobalInvocationID.x >= pc.data.maxBatchCount)
3337
return;
3438

39+
mvpBuff.mvps[gl_GlobalInvocationID.x] = pc.data.viewProjMatrix;
40+
41+
if (bool(pc.data.freezeCulling))
42+
return;
43+
3544
const CullData_t batchCullData = cullData[gl_GlobalInvocationID.x];
3645

3746
const mat2x3 bbox = mat2x3(batchCullData.aabbMinEdge,batchCullData.aabbMaxEdge);

examples_tests/41.VisibilityBuffer/main.cpp

Lines changed: 57 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,36 @@ using namespace nbl::core;
1717
using namespace nbl::asset;
1818
using namespace nbl::video;
1919

20+
bool freezeCulling = false;
21+
22+
class MyEventReceiver : public QToQuitEventReceiver
23+
{
24+
public:
25+
26+
MyEventReceiver()
27+
{
28+
}
29+
30+
bool OnEvent(const SEvent& event)
31+
{
32+
if (event.EventType == nbl::EET_KEY_INPUT_EVENT && !event.KeyInput.PressedDown)
33+
{
34+
switch (event.KeyInput.Key)
35+
{
36+
case nbl::KEY_KEY_Q: // so we can quit
37+
return QToQuitEventReceiver::OnEvent(event);
38+
case nbl::KEY_KEY_C: // freeze culling
39+
freezeCulling = !freezeCulling; // Not enabled/necessary yet
40+
return true;
41+
default:
42+
break;
43+
}
44+
}
45+
46+
return false;
47+
}
48+
};
49+
2050
#include "common.h"
2151
#include "rasterizationCommon.h"
2252

@@ -164,6 +194,7 @@ struct CullShaderData
164194
{
165195
core::smart_refctd_ptr<IGPUBuffer> perBatchCull;
166196
core::smart_refctd_ptr<IGPUBuffer> commandBuffer;
197+
core::smart_refctd_ptr<IGPUBuffer> mvpBuffer;
167198

168199
core::smart_refctd_ptr<IGPUComputePipeline> cullPipeline;
169200
core::smart_refctd_ptr<IGPUDescriptorSetLayout> cullDSLayout;
@@ -221,7 +252,7 @@ int main()
221252

222253
//! Since our cursor will be enslaved, there will be no way to close the window
223254
//! So we listen for the "Q" key being pressed and exit the application
224-
QToQuitEventReceiver receiver;
255+
MyEventReceiver receiver;
225256
device->setEventReceiver(&receiver);
226257

227258
auto* driver = device->getVideoDriver();
@@ -566,7 +597,7 @@ int main()
566597

567598
batchCullDataEnd->drawCommandGUID = packedMeshBufferData.mdiParameterOffset + i;
568599

569-
draw3DLine->enqueueBox(dbgLines, aabbs[aabbIdx], 1.0f, 0.0f, 0.0f, 1.0f, core::matrix3x4SIMD());
600+
draw3DLine->enqueueBox(dbgLines, aabbs[aabbIdx], 0.0f, 0.0f, 0.0f, 1.0f, core::matrix3x4SIMD());
570601

571602
batchCullDataEnd++;
572603
aabbIdx++;
@@ -608,7 +639,8 @@ int main()
608639

609640
cullShaderData.commandBuffer = gpump->getPackerDataStore().MDIDataBuffer;
610641
cullShaderData.maxBatchCount = std::distance(batchCullData.begin(), batchCullDataEnd);
611-
cullShaderData.perBatchCull = driver->createFilledDeviceLocalGPUBufferOnDedMem(cullShaderData.maxBatchCount, batchCullData.data());
642+
cullShaderData.perBatchCull = driver->createFilledDeviceLocalGPUBufferOnDedMem(cullShaderData.maxBatchCount * sizeof(CullData_t), batchCullData.data());
643+
cullShaderData.mvpBuffer = driver->createDeviceLocalGPUBufferOnDedMem(cullShaderData.maxBatchCount * sizeof(core::matrix4SIMD));
612644
}
613645
mesh_raw->convertToDummyObject(~0u);
614646

@@ -789,11 +821,12 @@ int main()
789821
}
790822
}
791823

824+
// cull shader ds
792825
{
793826
SPushConstantRange range{ ISpecializedShader::ESS_COMPUTE,0u,sizeof(CullShaderData_t) };
794827

795828
{
796-
IGPUDescriptorSetLayout::SBinding bindings[2];
829+
IGPUDescriptorSetLayout::SBinding bindings[3];
797830
bindings[0].binding = 0u;
798831
bindings[0].count = 1u;
799832
bindings[0].samplers = nullptr;
@@ -805,12 +838,18 @@ int main()
805838
bindings[1].samplers = nullptr;
806839
bindings[1].stageFlags = ISpecializedShader::ESS_COMPUTE;
807840
bindings[1].type = EDT_STORAGE_BUFFER;
808-
841+
842+
bindings[2].binding = 2u;
843+
bindings[2].count = 1u;
844+
bindings[2].samplers = nullptr;
845+
bindings[2].stageFlags = ISpecializedShader::ESS_COMPUTE;
846+
bindings[2].type = EDT_STORAGE_BUFFER;
847+
809848
cullShaderData.cullDSLayout = driver->createGPUDescriptorSetLayout(bindings, bindings + sizeof(bindings) / sizeof(IGPUDescriptorSetLayout::SBinding));
810849
}
811850

812851
{
813-
IGPUDescriptorSet::SDescriptorInfo infos[2];
852+
IGPUDescriptorSet::SDescriptorInfo infos[3];
814853

815854
infos[0].desc = core::smart_refctd_ptr(cullShaderData.perBatchCull);
816855
infos[0].buffer.offset = 0u;
@@ -819,12 +858,16 @@ int main()
819858
infos[1].desc = core::smart_refctd_ptr(cullShaderData.commandBuffer);
820859
infos[1].buffer.offset = 0u;
821860
infos[1].buffer.size = cullShaderData.commandBuffer->getSize();
822-
861+
862+
infos[2].desc = core::smart_refctd_ptr(cullShaderData.mvpBuffer);
863+
infos[2].buffer.offset = 0u;
864+
infos[2].buffer.size = cullShaderData.mvpBuffer->getSize();
865+
823866
cullShaderData.cullDS = driver->createGPUDescriptorSet(smart_refctd_ptr(cullShaderData.cullDSLayout));
824867

825-
IGPUDescriptorSet::SWriteDescriptorSet writes[2];
868+
IGPUDescriptorSet::SWriteDescriptorSet writes[3];
826869

827-
for (uint32_t i = 0u; i < 2; i++)
870+
for (uint32_t i = 0u; i < 3; i++)
828871
{
829872
writes[i].dstSet = cullShaderData.cullDS.get();
830873
writes[i].binding = i;
@@ -849,15 +892,15 @@ int main()
849892
cullShaderData.cullPipeline = driver->createGPUComputePipeline(nullptr, std::move(cullPipelineLayout), std::move(gpuCullShader));
850893
}
851894

852-
auto cullBatches = [&driver, &cullShaderData](const core::matrix4SIMD& vp)
895+
auto cullBatches = [&driver, &cullShaderData](const core::matrix4SIMD& vp, bool freezeCulling)
853896
{
854897
driver->bindDescriptorSets(EPBP_COMPUTE, cullShaderData.cullPipeline->getLayout(), 0u, 1u, &cullShaderData.cullDS.get(), nullptr);
855898
driver->bindComputePipeline(cullShaderData.cullPipeline.get());
856899

857900
CullShaderData_t cullPushConstants;
858901
cullPushConstants.viewProjMatrix = vp;
859-
cullPushConstants.viewProjDeterminant = core::determinant(vp);
860-
cullPushConstants.maxBatchCount = cullShaderData.maxBatchCount; //TODO: this should be uniform, and set only once
902+
cullPushConstants.maxBatchCount = cullShaderData.maxBatchCount;
903+
cullPushConstants.freezeCulling = static_cast<uint32_t>(freezeCulling);
861904

862905
driver->pushConstants(cullShaderData.cullPipeline->getLayout(), ISpecializedShader::ESS_COMPUTE, 0u, sizeof(CullShaderData_t), &cullPushConstants);
863906

@@ -875,10 +918,8 @@ int main()
875918
camera->setFarValue(5000.0f);
876919

877920
smgr->setActiveCamera(camera);
878-
879-
//tmp shit
880-
core::matrix4SIMD vpFromFirstFrame;
881921

922+
bool asdf = true;
882923
uint64_t lastFPSTime = 0;
883924
while (device->run() && receiver.keepOpen())
884925
{
@@ -888,17 +929,14 @@ int main()
888929
camera->OnAnimate(std::chrono::duration_cast<std::chrono::milliseconds>(device->getTimer()->getTime()).count());
889930
camera->render();
890931

891-
if (lastFPSTime == 0)
892-
vpFromFirstFrame = camera->getConcatenatedMatrix();
893-
894932
SBasicViewParameters uboData;
895933
memcpy(uboData.MVP, camera->getConcatenatedMatrix().pointer(), sizeof(core::matrix4SIMD));
896934
memcpy(uboData.MV, camera->getViewMatrix().pointer(), sizeof(core::matrix3x4SIMD));
897935
memcpy(uboData.NormalMat, camera->getViewMatrix().pointer(), sizeof(core::matrix3x4SIMD));
898936
driver->updateBufferRangeViaStagingBuffer(sceneData.ubo.get(), 0u, sizeof(SBasicViewParameters), &uboData);
899937

900938
// cull MDIs
901-
cullBatches(vpFromFirstFrame);
939+
cullBatches(camera->getConcatenatedMatrix(), freezeCulling);
902940
COpenGLExtensionHandler::pGlMemoryBarrier(GL_COMMAND_BARRIER_BIT | GL_SHADER_STORAGE_BARRIER_BIT);
903941

904942
driver->setRenderTarget(visBuffer);

examples_tests/41.VisibilityBuffer/rasterizationCommon.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,16 @@
66
struct CullShaderData_t
77
{
88
mat4 viewProjMatrix;
9-
float viewProjDeterminant;
109
uint maxBatchCount;
10+
uint freezeCulling;
1111
};
1212

1313
struct CullData_t
1414
{
1515
vec3 aabbMinEdge;
16+
uint padding;
1617
vec3 aabbMaxEdge;
1718
uint drawCommandGUID;
1819
};
1920

20-
struct DrawData_t
21-
{
22-
mat4 MVP;
23-
uint backfacingBit_batchInstanceGUID;
24-
uint firstIndex;
25-
uint padding1;
26-
uint padding2;
27-
};
28-
2921
#endif

0 commit comments

Comments
 (0)