From 2644b127fa7a2d54498138fd991e7e4302e22bf0 Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Fri, 7 Jun 2024 18:43:10 +0200 Subject: [PATCH] Draw List works now --- res/shaders/DepthCullingMesh.slang | 2 +- res/shaders/DepthCullingTask.slang | 2 +- res/shaders/VisibilityCompute.slang | 24 +++++------- .../Graphics/RenderPass/VisibilityPass.cpp | 37 +++++++++++-------- src/Engine/Window/GameView.cpp | 6 +++ 5 files changed, 38 insertions(+), 33 deletions(-) diff --git a/res/shaders/DepthCullingMesh.slang b/res/shaders/DepthCullingMesh.slang index 841c1ef..d069280 100644 --- a/res/shaders/DepthCullingMesh.slang +++ b/res/shaders/DepthCullingMesh.slang @@ -38,7 +38,7 @@ void meshMain( uint local_idx1 = pScene.primitiveIndices[m.primitiveOffset + (p * 3) + 1]; uint local_idx2 = pScene.primitiveIndices[m.primitiveOffset + (p * 3) + 2]; indices[p] = uint3(local_idx0, local_idx1, local_idx2); - prim[p].cull = false;//!cull.triangleCulled(p); + prim[p].cull = !cull.triangleCulled(p); #ifdef VISIBILITY prim[p].prim = encodePrimitive(p, meshletId); #endif diff --git a/res/shaders/DepthCullingTask.slang b/res/shaders/DepthCullingTask.slang index 04aab52..1c0d119 100644 --- a/res/shaders/DepthCullingTask.slang +++ b/res/shaders/DepthCullingTask.slang @@ -26,7 +26,7 @@ void taskMain( uint cull = p.cullingOffset + i; MeshletDescription meshlet = pScene.meshletInfos[m]; MeshletCullingInfo culling = pScene.cullingInfos[cull]; - //if(!culling.anyVisible()) + if(false)//if(!culling.anyVisible()) { uint index; InterlockedAdd(head, 1, index); diff --git a/res/shaders/VisibilityCompute.slang b/res/shaders/VisibilityCompute.slang index 9824335..ce5c079 100644 --- a/res/shaders/VisibilityCompute.slang +++ b/res/shaders/VisibilityCompute.slang @@ -4,26 +4,20 @@ import Scene; struct VisibilityCullingData { Texture2D visibilityTexture; - RWStructuredBuffer cullingInfos; + globallycoherent RWStructuredBuffer cullingInfos; }; ParameterBlock pVisibilityParams; -[numthreads(BLOCK_SIZE, 1, 1)] +[numthreads(BLOCK_SIZE, BLOCK_SIZE, 1)] [shader("compute")] void computeMain( uint3 dispatchThreadID: SV_DispatchThreadID, ){ - //int3 texCoords = int3(dispatchThreadID.xy, 0); - //uint encoded = pVisibilityParams.visibilityTexture.Load(texCoords).r; - //uint2 decoded = decodePrimitive(encoded); - //uint arrIdx = decoded.x / 32; - //uint bit = decoded.x % 32; - pVisibilityParams.cullingInfos[dispatchThreadID.x].visible[0] = 0x0; - pVisibilityParams.cullingInfos[dispatchThreadID.x].visible[1] = 0x0; - pVisibilityParams.cullingInfos[dispatchThreadID.x].visible[2] = 0x0; - pVisibilityParams.cullingInfos[dispatchThreadID.x].visible[3] = 0x0; - pVisibilityParams.cullingInfos[dispatchThreadID.x].visible[4] = 0x0; - pVisibilityParams.cullingInfos[dispatchThreadID.x].visible[5] = 0x0; - pVisibilityParams.cullingInfos[dispatchThreadID.x].visible[6] = 0x0; - pVisibilityParams.cullingInfos[dispatchThreadID.x].visible[7] = 0x0; + int3 texCoords = int3(dispatchThreadID.xy, 0); + uint encoded = pVisibilityParams.visibilityTexture.Load(texCoords).r; + uint2 decoded = decodePrimitive(encoded); + uint arrIdx = decoded.x / 32; + uint bit = decoded.x % 32; + uint32_t orig; + InterlockedOr(pVisibilityParams.cullingInfos[decoded.y].visible[arrIdx], (1 << bit), orig); } \ No newline at end of file diff --git a/src/Engine/Graphics/RenderPass/VisibilityPass.cpp b/src/Engine/Graphics/RenderPass/VisibilityPass.cpp index 61070be..68dde9a 100644 --- a/src/Engine/Graphics/RenderPass/VisibilityPass.cpp +++ b/src/Engine/Graphics/RenderPass/VisibilityPass.cpp @@ -3,6 +3,8 @@ using namespace Seele; +extern bool resetVisibility; + VisibilityPass::VisibilityPass(Gfx::PGraphics graphics, PScene scene) : RenderPass(graphics, scene) { @@ -17,22 +19,26 @@ void VisibilityPass::beginFrame(const Component::Camera& cam) { RenderPass::beginFrame(cam); cullingBuffer->rotateBuffer(VertexData::getMeshletCount() * sizeof(VertexData::MeshletCullingInfo), true); + if (resetVisibility) + { + Array cullingData(VertexData::getMeshletCount()); + std::memset(cullingData.data(), 0xffff, cullingData.size() * sizeof(VertexData::MeshletCullingInfo)); - //Array cullingData(VertexData::getMeshletCount()); - //std::memset(cullingData.data(), 0xffff, cullingData.size() * sizeof(VertexData::MeshletCullingInfo)); + cullingBuffer->updateContents(ShaderBufferCreateInfo{ + .sourceData = { + .size = VertexData::getMeshletCount() * sizeof(VertexData::MeshletCullingInfo), + .data = (uint8 *)cullingData.data(), + }, + .numElements = VertexData::getMeshletCount()}); + cullingBuffer->pipelineBarrier( + Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, + Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT, + Gfx::SE_ACCESS_MEMORY_WRITE_BIT, + Gfx::SE_PIPELINE_STAGE_TOP_OF_PIPE_BIT); + + resetVisibility = false; + } - //cullingBuffer->updateContents(ShaderBufferCreateInfo{ - // .sourceData = { - // .size = VertexData::getMeshletCount() * sizeof(VertexData::MeshletCullingInfo), - // .data = (uint8 *)cullingData.data(), - // }, - // .numElements = VertexData::getMeshletCount()}); - //cullingBuffer->pipelineBarrier( - // Gfx::SE_ACCESS_TRANSFER_WRITE_BIT, - // Gfx::SE_PIPELINE_STAGE_TRANSFER_BIT, - // Gfx::SE_ACCESS_MEMORY_WRITE_BIT, - // Gfx::SE_PIPELINE_STAGE_TOP_OF_PIPE_BIT); - } void VisibilityPass::render() @@ -60,8 +66,7 @@ void VisibilityPass::render() Gfx::OComputeCommand command = graphics->createComputeCommand("VisibilityCommand"); command->bindPipeline(visibilityPipeline); command->bindDescriptor({viewParamsSet, visibilitySet}); - command->dispatch(VertexData::getMeshletCount() / BLOCK_SIZE, 1, 1); - //command->dispatch(threadGroupSize.x, threadGroupSize.y, threadGroupSize.z); + command->dispatch(threadGroupSize.x, threadGroupSize.y, threadGroupSize.z); Array commands; commands.add(std::move(command)); graphics->executeCommands(std::move(commands)); diff --git a/src/Engine/Window/GameView.cpp b/src/Engine/Window/GameView.cpp index 22011f9..a4d38b9 100644 --- a/src/Engine/Window/GameView.cpp +++ b/src/Engine/Window/GameView.cpp @@ -20,6 +20,8 @@ using namespace Seele; bool usePositionOnly = false; bool useViewCulling = false; bool useLightCulling = false; +bool resetVisibility = false; + GameView::GameView(Gfx::PGraphics graphics, PWindow window, const ViewportCreateInfo &createInfo, std::string dllPath) : View(graphics, window, createInfo, "Game") @@ -122,6 +124,10 @@ void GameView::keyCallback(KeyCode code, InputAction action, KeyModifier modifie useLightCulling = !useLightCulling; std::cout << "Use Light Culling " << useLightCulling << std::endl; } + if (code == KeyCode::KEY_R && action == InputAction::RELEASE) + { + resetVisibility = true; + } } void GameView::mouseMoveCallback(double xPos, double yPos)